[英]self incrementing array destruction? Node.js
例,
for (var i = 0; i < 5; i ++)
{
console.log(jsonfile[i].Username)
}
我嘗試了此操作,並得到以下結果:
TypeError: Cannot read property 'Username' of undefined
我的JSON是:
[
{
"Username": "ozziep",
"ProjectID": "ExpressJS",
"TimeStamp": "2016-12-30T19:54:52.418Z",
"Comments": "hello world how are we today?"
}
]
我正在使用require加載JSON。
由於您的jsonfile
只有一個對象,因此jsonFile[1]
將是未定義的。
代替i < 5
使用i < jsonfile.length
如下所示:
for (var i = 0; i < jsonfile.length; i ++)
{
console.log(jsonfile[i].Username)
}
這可以解決問題,但謝謝大家的投入。
for( var user in jsonfile) {
console.log(jsonfile[user].Username);
console.log("------------------------")
console.log(jsonfile[user].Comments);
console.log("------------------------")
console.log(jsonfile[user].ProjectID);
console.log("------------------------")
console.log("")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.