假设我已经定义了一个像这样的对象数组:
var person = [{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}, {firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}];
现在正在做
console.log(person);
returns "[Object,Object]"
我想打印数组的细节。我该怎么做?
答案 0 :(得分:4)
答案 1 :(得分:3)
console.log(JSON.stringify(person));
这是一种方法。
答案 2 :(得分:2)
var person = [{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}, {firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}] ;
console.log(JSON.stringify(person));
//if you want to parse
for (var i = 0; i < person.length; i++) {
var object = person[i];
for (var property in object) {
alert('item ' + i + ': ' + property + '=' + object[property]);
}
}
这是你想要的吗?如果没有请详细说明。
答案 3 :(得分:1)
试试这个:
const something = JSON.stringify([
{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"},
{firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}
], null, " ");
document.querySelector("#result").textContent = something;
// or in the console
console.log(something);
<pre id="result"></pre>
答案 4 :(得分:1)
你可以这样做:
var person = [{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"},
{firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}];
JSON.stringify(person);
答案 5 :(得分:1)
正如其他人已经指出的那样,您可以使用JSON.stringify
。但是,您的JSON显示可折叠的对象,因此您可以在那里获得信息。如果您想进一步美化输出,可以尝试pretty-js或underscore.js。