Ваша проблема возникла из-за этого l oop:
for (var j = 0; j < col.length; j++) {
// ...
}
Вы можете проверить, когда col[j]
равно values
, чтобы получить MSSBL
или SNOW
значение:
for (var j = 0; j < col.length; j++) {
var tabCell = tr.insertCell(-1);
if (col[j] === 'values') {
var item = attributes[i][col[j]];
console.log('MSSBL:', item.MSSBL);
console.log('SNOW:', item.SNOW);
// try to append MSSBL value:
tabCell.innerHTML = item.MSSBL;
} else {
// other properties
tabCell.innerHTML = attributes[i][col[j]];
}
}
Обновление: в своем обновленном коде вы можете редактировать его внутри l oop:
for (key in attributes[i]) {
if (key === 'values') {
document.write('<td>' + attributes[i][key]['MSSBL'] + '</td>');
} else {
document.write('<td>' + attributes[i][key] + '</td>');
}
}
var attributes =
[
{
"name": "Asset #",
"display_name": "Asset #",
"key": "header2",
"values": {
"MSSBL": "4-194169767930##1-2H77NZSQ",
"SNOW": "4-194169767930##1-2H77NZSQ"},
"type": "header",
"data_type": "Text",
"editable": "N"
},
{
"name": "Serial Number",
"display_name": "Serial Number",
"key": "header3",
"values": {
"MSSBL": "21256112##1-2H77NZSQ",
"SNOW": "NA##1-2H77NZSQ"},
"type": "header",
"data_type": "Text",
"editable": "N"
},
{
"name": "ACCOUNT NUMBER",
"display_name": "ACCOUNT NUMBER",
"key": "header6",
"values": { "MSSBL": "532649##1-2H77NZSQ",
"SNOW": "NA##1-2H77NZSQ"},
"type": "header",
"data_type": "Text",
"editable": "N"
}
]
var key = [];
document.write("<table border==\"1\"><tr>");
for (key in attributes[0]) {
document.write('<td>' + '<b>' + key + '</b>' + '</td>');
}
document.write("</tr>");
for (var i = 0; i < attributes.length; i++) {
document.write('<tr>');
for (key in attributes[i]) {
if (key === 'values') {
document.write('<td>' + attributes[i][key]['MSSBL'] + '</td>');
} else {
document.write('<td>' + attributes[i][key] + '</td>');
}
}
document.write('</tr>');
}
document.write("</table>");