簡體   English   中英

自增數組破壞? Node.js的

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM