在下(新)行显示嵌套对象属性
Display nested objects properties in next (new) line
我有对象数组,其中有子对象(嵌套对象)。整个结构看起来与此类似(所有子对象具有相同的标签,只有值不同):
var arr = [
obj0 = {
subObj0: {
name: "John",
lastName: "Doe",
age: 50,
nick: "JD",
phone: 123456
},
subObj1: {
name: "Jane",
lastName: "Dee",
age: 30,
nick: "lady",
phone: 654098
},
subObj2: {
name: "Ash",
lastName: "Bash",
age: 33,
nick: "asdB",
phone: 987123
}
},
obj1 = {
subObj0: {
name: "Asd",
lastName: "Dsa",
age: 10,
nick: "none",
phone: 12
},
subObj1: {
name: "Ivy",
lastName: "Mash",
age: 3,
nick: "IvMash",
phone: 9823
}
}
];
我试图在控制台中像这样显示这些子对象:
for (index in arr)
{
for (index2 in arr[index])
{
console.log(JSON.stringify(arr[index][index2],null,4));
}
}
,它给了我这些对象和子对象的树的输出。
如何在控制台中将其显示为每个子对象都在新行中,如下所示?
所以没有逗号和双引号,只有"label: value"形式。
很少replace()
应该这样做。单击 运行 按钮查看结果。
var arr = [
{
subObj0: {
name: "John",
lastName: "Doe",
age: 50,
nick: "JD",
phone: 123456
},
subObj1: {
name: "Jane",
lastName: "Dee",
age: 30,
nick: "lady",
phone: 654098
},
subObj2: {
name: "Ash",
lastName: "Bash",
age: 33,
nick: "asdB",
phone: 987123
}
},
{
subObj0: {
name: "Asd",
lastName: "Dsa",
age: 10,
nick: "none",
phone: 12
},
subObj1: {
name: "Ivy",
lastName: "Mash",
age: 3,
nick: "IvMash",
phone: 9823
}
}
];
var output = [];
arr.forEach(function (obj, i) {
output.push('obj' + i + ':');
for (var key in obj) {
var value = obj[key];
var str = JSON.stringify(value)
.replace(/[{}"]/g, '') // remove {} and "
.replace(/[,]/g, '\t') // replace commas by tabulations
.replace(/:/g, ': '); // add a nice space after :
output.push(key + ': ' + str);
}
})
output = output.join('\n');
console.log(output);
document.write('<pre>' + output + '</pre>');
我有对象数组,其中有子对象(嵌套对象)。整个结构看起来与此类似(所有子对象具有相同的标签,只有值不同):
var arr = [
obj0 = {
subObj0: {
name: "John",
lastName: "Doe",
age: 50,
nick: "JD",
phone: 123456
},
subObj1: {
name: "Jane",
lastName: "Dee",
age: 30,
nick: "lady",
phone: 654098
},
subObj2: {
name: "Ash",
lastName: "Bash",
age: 33,
nick: "asdB",
phone: 987123
}
},
obj1 = {
subObj0: {
name: "Asd",
lastName: "Dsa",
age: 10,
nick: "none",
phone: 12
},
subObj1: {
name: "Ivy",
lastName: "Mash",
age: 3,
nick: "IvMash",
phone: 9823
}
}
];
我试图在控制台中像这样显示这些子对象:
for (index in arr)
{
for (index2 in arr[index])
{
console.log(JSON.stringify(arr[index][index2],null,4));
}
}
,它给了我这些对象和子对象的树的输出。
如何在控制台中将其显示为每个子对象都在新行中,如下所示?
所以没有逗号和双引号,只有"label: value"形式。
很少replace()
应该这样做。单击 运行 按钮查看结果。
var arr = [
{
subObj0: {
name: "John",
lastName: "Doe",
age: 50,
nick: "JD",
phone: 123456
},
subObj1: {
name: "Jane",
lastName: "Dee",
age: 30,
nick: "lady",
phone: 654098
},
subObj2: {
name: "Ash",
lastName: "Bash",
age: 33,
nick: "asdB",
phone: 987123
}
},
{
subObj0: {
name: "Asd",
lastName: "Dsa",
age: 10,
nick: "none",
phone: 12
},
subObj1: {
name: "Ivy",
lastName: "Mash",
age: 3,
nick: "IvMash",
phone: 9823
}
}
];
var output = [];
arr.forEach(function (obj, i) {
output.push('obj' + i + ':');
for (var key in obj) {
var value = obj[key];
var str = JSON.stringify(value)
.replace(/[{}"]/g, '') // remove {} and "
.replace(/[,]/g, '\t') // replace commas by tabulations
.replace(/:/g, ': '); // add a nice space after :
output.push(key + ': ' + str);
}
})
output = output.join('\n');
console.log(output);
document.write('<pre>' + output + '</pre>');