Advertisement
bebo231312312321

Untitled

Mar 25th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fromJsonToHtml(input) {
  2.     let arrayOfObjects = JSON.parse(input);
  3.  
  4.     console.log('<table>');
  5.  
  6.     let tableHead = '<tr>';
  7.  
  8.     for (let key in arrayOfObjects[0]) {
  9.         tableHead += `<th>${escaper(key).trim()}</th>`;
  10.     }
  11.     tableHead += '</tr>';
  12.  
  13.     console.log(tableHead);
  14.  
  15.     for (let el of arrayOfObjects) {
  16.         let tbody = '<tr>';
  17.         for (let key in el) {
  18.             tbody += `<td>${escaper(el[key])}</td>`;
  19.         }
  20.         tbody += '</tr>';
  21.         console.log(tbody);
  22.     }
  23.  
  24.     function escaper(value) {
  25.         return value
  26.             .toString()
  27.             .replace(/&/g, '&amp;')
  28.             .replace(/</g, '&lt;')
  29.             .replace(/>/g, '&gt;')
  30.             .replace(/"/g, '&quot;')
  31.             .replace(/'/g, '&#39;');
  32.     }
  33.  
  34.     console.log('</table>');
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement