Advertisement
kstoyanov

02. JSON's Table

Sep 21st, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function jsonTable(input) {
  2.   let html = '<table>\n';
  3.  
  4.   const htmlEscape = (text) => {
  5.     const map = {
  6.       '"': '&quot;',
  7.       '&': '&amp;',
  8.       "'": '&#39;',
  9.       '<': '&lt;',
  10.       '>': '&gt;',
  11.     };
  12.     return text.replace(/[\"&'<>]/g, (ch) => map[ch]);
  13.   };
  14.  
  15.  
  16.   input.forEach((line) => {
  17.     const obj = JSON.parse(line);
  18.     html += '    <tr>\n';
  19.     html += `       <td>${htmlEscape(obj.name)}</td>\n`;
  20.     html += `       <td>${htmlEscape(obj.position)}</td>\n`;
  21.     html += `       <td>${obj.salary}</td>\n`;
  22.     html += '    </tr>\n';
  23.   });
  24.  
  25.   html += '</table>';
  26.  
  27.   console.log(html);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement