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 += "
"
str += ""
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 += " | ";
else{
str += "" + key[i] + " | ";
}
}
str += "
";
for (var i = 0; i < persons.length; i++) {
var key = Object.keys(persons[i]);
if(!(i % 2))
str += ""
else
str += "
"
for (var c = 0; c < key.length; c++) {
if(c < 3)
str += "" + persons[i][key[c]] + " | ";
if(c >= 3 && key[c] in persons[i])
str += "" + persons[i][key[c]] + " | ";
}
str += "
"
}
str += "
";
document.write(str);