elena1234

From JSON to HTML table - JavaScript

Aug 24th, 2021 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // input: ['[{"Name":"Pesho <div>-a","Age":20,"City":"Sofia"},
  2. // {"Name":"Gosho","Age":18,"City":"Plovdiv"},{"Name":"Angel","Age":18,"City":"Veliko Tarnovo"}]']
  3.  
  4. function solve(input) {
  5.     let jsonData = input.shift();
  6.     let students = JSON.parse(jsonData);
  7.     let firstObject = students[0];
  8.  
  9.     let html = '<table>';
  10.     html+= `<tr>${Object.keys(firstObject).map(x => `<th>${x}</th>`).join('')}</tr>`;
  11.  
  12.     students.forEach(student =>{
  13.         html+= `<tr>${Object.values(student).map(x => `<td>${x}</td>`).join('')}</tr>`;
  14.     })
  15.  
  16.     html += '</table>';
  17.     console.log(html);
  18. }
  19.  
  20. solve(
  21. ['[{"Name":"Pesho <div>-a","Age":20,"City":"Sofia"},{"Name":"Gosho","Age":18,"City":"Plovdiv"},{"Name":"Angel","Age":18,"City":"Veliko Tarnovo"}]']
  22. )
Add Comment
Please, Sign In to add comment