简体   繁体   English

使用循环访问嵌套对象但未定义太多?

[英]Accessing a nested object with loop but too many undefines?

I'm working with an object literal and trying to access the nested array and iterate through a loop but it keeps displaying 59 times and 5 times again as undefined in my console but then displays the arrays. 我正在使用对象文字,并尝试访问嵌套数组并遍历循环,但是在我的控制台中,它不断显示未定义的59次和5次,然后显示数组。 How do I stop it from doing the undefined in the console? 如何阻止它在控制台中执行未定义的操作? Thanks! 谢谢!

控制台日志的屏幕截图

var votingData = {
  candidates: [{
  name: "Harry S. Truman",
  party: "Democratic"
},
{
  name: "Thomas E. Dewey",
  party: "Republican"
},
{
  name: "Strom Thurmond",
  party: "Dixiecrat"
}]

}

for(var candidate in votingData) {
  if(votingData.hasOwnProperty(candidate)) {
    for (let i = 0, j = votingData[candidate].length; i < j; i++) {
    console.log(votingData[candidate][i].name, votingData[candidate]
    [i].party);
   }
 }
}

Your for/in loop is contributing to the problem as it is not needed because votingData contains only one property, candidates . 您的for/in循环正在导致此问题,因为这是不需要的,因为votingData仅包含一个属性( candidates Since there's just one property, you can access it directly, no need for a loop. 由于只有一个属性,因此您可以直接访问它,而无需循环。

You only need to loop through the arrays which are in the votingData.candidates property and for that, you can either use a standard counting for loop, which you are doing or, even better, use the Array.forEach() mechanism for looping. 你只能通过它们在阵列需要循环votingData.candidates财产,并且,你可以使用一个标准计数for循环,你正在做的,或者甚至更好,使用Array.forEach()机制循环。 It's better because it gives you direct access to the array element being looped over, without the need for an indexer and that allows the syntax to be much clearer, which in turn, tends to avoid bugs like this. 更好是因为它使您可以直接访问要循环的数组元素,而无需使用索引器,并且可以使语法更加清晰,从而可以避免此类错误。

 var votingData = { candidates: [{ name: "Harry S. Truman", party: "Democratic" }, { name: "Thomas E. Dewey", party: "Republican" }, { name: "Strom Thurmond", party: "Dixiecrat" }], }; // Just loop through the arrays in the votingData.candidates property votingData.candidates.forEach(function(candidate) { // Now "candidate" is an object, so standard "dot notation" to access // any/all properties of the current object is the way to go. console.log(candidate.name, candidate.party); }); 

Your first for loop and if statement are not necessary. 不需要您的第一个for循环和if语句。 You're probably getting a lot of undefined because you have another property that's getting counted in the for-in statement. 您可能会得到很多未定义的信息,因为您有另一个要在for-in语句中计数的属性。

Here's an example that doesn't use forEach if you prefer it. 如果您愿意,这是一个不使用forEach的示例。

EDIT 编辑

Formattied the data to improve readability 格式化数据以提高可读性

 var votingData = { candidates: [ { name: "Harry S. Truman", party: "Democratic" }, { name: "Thomas E. Dewey", party: "Republican" }, { name: "Strom Thurmond", party: "Dixiecrat" } ], not_candidates : [] } for (let i = 0, j = votingData.candidates.length; i < j; i++) { console.log(votingData.candidates[i].name, votingData.candidates[i].party); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM