14_HTML_th_optional.js 980 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. let persons = [
  2. {
  3. name: 'Sergey',
  4. surname: 'Honcharenko',
  5. age: 28,
  6. isMarried: false,
  7. },
  8. {
  9. name: 'Andrew',
  10. surname: 'Klymchuk',
  11. middleName: 'Anatoliyovych',
  12. haveAJob: true,
  13. },
  14. {
  15. name: 'Alexander',
  16. surname: 'Slobodian',
  17. sex: 'male',
  18. hasAPet: false,
  19. }
  20. ];
  21. let personsKeys = [];
  22. for (let key in persons) {
  23. for (let value in persons[key]) {
  24. personsKeys.push(value);
  25. }
  26. }
  27. let personsUniqueKeys = new Set(personsKeys);
  28. let str = "<table>";
  29. str += `<thead><tr>`;
  30. personsUniqueKeys.forEach(item => {
  31. str += `<th>${item}</th>`;
  32. });
  33. str += "</thead></tr>";
  34. for (let key in persons) {
  35. str += `<tr>`;
  36. for (let personsKey in [...personsUniqueKeys]) {
  37. if(Object.keys(persons[key]).includes([...personsUniqueKeys][personsKey])) {
  38. str += `<td>${persons[key][[...personsUniqueKeys][personsKey]]}</td>`
  39. } else {
  40. str += `<td>-</td>`
  41. }
  42. }
  43. str += "</tr>";
  44. }
  45. str += "</table>";
  46. document.write(str);