![](/img/trans.png)
[英]How to display all the properties of an object from an array of objects in javascript?
[英]How to display all the inputs of an array of objects in Javascript?
假設我已經定義了一個對象數組,如下所示:
var person = [{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}, {firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}];
現在做
console.log(person);
returns "[Object,Object]"
我想打印數組的細節。 我該怎么做呢?
console.log(JSON.stringify(person));
這是一種方法。
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]); } }
這是你想要的嗎? 如果沒有請詳細說明。
嘗試這個:
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>
你可以這樣做:
var person = [{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"},
{firstName:"Arunima", lastName:"Ghosh", age:20, eyeColor:"black"}];
JSON.stringify(person);
正如其他人已經指出的那樣,您可以使用JSON.stringify
。 但是,您的JSON顯示可折疊的對象,因此您可以在那里獲得信息。 如果你想進一步美化你的輸出,你可以試試pretty-js或underscore.js 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.