hw07_18.html 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <header>
  2. <h1>Table</h1>
  3. </header>
  4. <body>
  5. <script>
  6. var arr = [
  7. {
  8. "Name": "chevrolet chevelle malibu",
  9. "Cylinders": 8,
  10. "Displacement": 307,
  11. "Horsepower": 130,
  12. "Weight_in_lbs": 3504,
  13. "Origin": "USA"
  14. },
  15. {
  16. "Name": "buick skylark 320",
  17. "Miles_per_Gallon": 15,
  18. "Cylinders": 8,
  19. "Displacement": 350,
  20. "Horsepower": 165,
  21. "Weight_in_lbs": 3693,
  22. "Acceleration": 11.5,
  23. "Year": "1970-01-01",
  24. },
  25. {
  26. "Miles_per_Gallon": 18,
  27. "Cylinders": 8,
  28. "Displacement": 318,
  29. "Horsepower": 150,
  30. "Weight_in_lbs": 3436,
  31. "Year": "1970-01-01",
  32. "Origin": "USA"
  33. },
  34. {
  35. "Name": "amc rebel sst",
  36. "Miles_per_Gallon": 16,
  37. "Cylinders": 8,
  38. "Displacement": 304,
  39. "Horsepower": 150,
  40. "Year": "1970-01-01",
  41. "Origin": "USA"
  42. },
  43. ]
  44. names = Object
  45. .entries(
  46. arr.reduce(
  47. (prev, next) => {
  48. return { ...prev, ...next };
  49. }
  50. ))
  51. .map(entry => entry[0]);
  52. str = "<table>";
  53. str += "<tr>" +
  54. names
  55. .map(name => `<td>${name}</td>`)
  56. .join('') +
  57. "</tr>";
  58. str +=
  59. arr
  60. .map(obj =>
  61. `<tr>${
  62. names
  63. .map(name => `<td>${obj[name] || "-"}</td>`)
  64. .join('')
  65. }</tr>`)
  66. .join('');
  67. str += "</table>";
  68. document.write(str)
  69. </script>
  70. </body>