main.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. var a = {
  2. name: "Ivan",
  3. surname: "Ivanov",
  4. }
  5. var b = {
  6. name: "Petr",
  7. surname: "Petrov",
  8. }
  9. var c = {
  10. name: "Alex",
  11. surname: "Sidarov",
  12. }
  13. a.age = 20;
  14. b.fathername = "Nikolai";
  15. c.sex = "male";
  16. var persons = [a,b,c,
  17. {
  18. name: "pasha",
  19. surname: "efimenko",
  20. age: 22
  21. }
  22. ]
  23. var str = "";
  24. str += "<table cellpadding = 5>"
  25. str += "<tr>"
  26. for (var i = 0; i < persons.length - 2; i++) {
  27. var br;
  28. br = false;
  29. var key = Object.keys(persons[i]);
  30. for (var j = 0; j < persons.length; j++) {
  31. var keyy = Object.keys(persons[j]);
  32. if(key[i] !== keyy[i]) // Проверяю совпадает ли ключ объкта с ключами остальных, если нет то заголовок не выводится
  33. br = true;
  34. }
  35. if(br)
  36. str += "<th></th>";
  37. else{
  38. str += "<th>" + key[i] + "</th>";
  39. }
  40. }
  41. str += "</tr>";
  42. for (var i = 0; i < persons.length; i++) {
  43. var key = Object.keys(persons[i]);
  44. if(!(i % 2))
  45. str += "<tr style = 'background: #ccc;'>"
  46. else
  47. str += "<tr>"
  48. for (var c = 0; c < key.length; c++) {
  49. if(c < 3)
  50. str += "<td>" + persons[i][key[c]] + "</td>";
  51. if(c >= 3 && key[c] in persons[i])
  52. str += "<td>" + persons[i][key[c]] + "</td>";
  53. }
  54. str += "</tr>"
  55. }
  56. str += "</table>";
  57. document.write(str);