1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- var a = {
- name: "Ivan",
- surname: "Ivanov",
- }
- var b = {
- name: "Petr",
- surname: "Petrov",
- }
- var c = {
- name: "Alex",
- surname: "Sidarov",
- }
- a.age = 20;
- b.fathername = "Nikolai";
- c.sex = "male";
- var persons = [a,b,c,
- {
- name: "pasha",
- surname: "efimenko",
- age: 22
- }
- ]
- var str = "";
- str += "<table cellpadding = 5>"
- str += "<tr>"
- for (var i = 0; i < persons.length - 2; i++) {
- var br;
- br = false;
- var key = Object.keys(persons[i]);
- for (var j = 0; j < persons.length; j++) {
- var keyy = Object.keys(persons[j]);
- if(key[i] !== keyy[i]) // Проверяю совпадает ли ключ объкта с ключами остальных, если нет то заголовок не выводится
- br = true;
- }
- if(br)
- str += "<th></th>";
- else{
- str += "<th>" + key[i] + "</th>";
- }
- }
- str += "</tr>";
-
- for (var i = 0; i < persons.length; i++) {
- var key = Object.keys(persons[i]);
-
- if(!(i % 2))
- str += "<tr style = 'background: #ccc;'>"
- else
- str += "<tr>"
-
- for (var c = 0; c < key.length; c++) {
- if(c < 3)
- str += "<td>" + persons[i][key[c]] + "</td>";
- if(c >= 3 && key[c] in persons[i])
- str += "<td>" + persons[i][key[c]] + "</td>";
- }
- str += "</tr>"
- }
- str += "</table>";
- document.write(str);
|