Guest User

From JSON to HTML Table

a guest
Nov 18th, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve ([json]) {
  2.   let inputArr = JSON.parse(json)
  3.   let html = '<table>\n'
  4.   html += '  <tr>'
  5.   for (let key of Object.keys(inputArr[0])) {
  6.     html += `<th>${htmlEscape(key)}</th>`
  7.   }
  8.   html += '</tr>\n'
  9.  
  10.   for (let object of inputArr) {
  11.     html += '  <tr>'
  12.     for (let key of Object.keys(object)) {
  13.       html += `<td>${htmlEscape(object[key] + '')}</td>`
  14.     }
  15.     html += '</tr>\n'
  16.   }
  17.   html += '</table>'
  18.  
  19.   console.log(html)
  20.  
  21.   function htmlEscape (text) {
  22.     return text.replace(/&/g, '&amp;')
  23.       .replace(/</g, '&lt;')
  24.       .replace(/>/g, '&gt;')
  25.       .replace(/"/g, '&quot;')
  26.       .replace(/'/g, '&#39;')
  27.   }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment