简体   繁体   English

为什么迭代循环太多?

[英]Why is Iteration looping too many times?

I am trying to loop through the object matches for the value of matchId however when i use the loop when the match.length = 2 then console.log(i) returns 8 times? 我正在尝试遍历对象匹配中matchID的值,但是当我使用match.length = 2时,然后console.log(i)返回8次的循环吗?

here is my code: 这是我的代码:

lolapi.MatchList.getBySummonerId(obj['savisaar2'].id, options, function(err, matches) {
    for (var matchId in matches) {
      var game = matches.matches;

      for(var i = 0; i < game.length; i++) {
        console.log(i);
      }
    }
  });

the return of the object is as follows: 对象的返回如下:

{ matches: 
[ { region: 'OCE',
   platformId: 'OC1',
   matchId: 122934310,
   champion: 36,
   queue: 'TEAM_BUILDER_DRAFT_RANKED_5x5',
   season: 'SEASON2016',
   timestamp: 1456100362493,
   lane: 'BOTTOM',
   role: 'DUO_SUPPORT' },
 { region: 'OCE',
   platformId: 'OC1',
   matchId: 122510663,
   champion: 44,
   queue: 'TEAM_BUILDER_DRAFT_RANKED_5x5',
   season: 'SEASON2016',
   timestamp: 1455751169038,
   lane: 'BOTTOM',
   role: 'DUO_SUPPORT' } ],
startIndex: 0,
endIndex: 2,
totalGames: 135 }

thanks guys 多谢你们

It seems that your nested for loop within your foreach is going to trigger this as you'll iterate matches.length * match.games.length : 看来你的嵌套for循环您内foreach是要触发这个,你会遍历matches.length * match.games.length

// This will iterate for each match
for (var matchId in matches) {
    // Is this intentional (should this be matchId.matches?)
    var game = matches.matches;
    // This will iterate for each game (in each match)
    for(var i = 0; i < game.length; i++) {
       console.log(i);
    }
}

So if you have two matches and your loop iterates eight times, this means that there was eight games between your two match objects. 因此,如果您有两次比赛,并且循环迭代八次,则意味着两个match对象之间有八场match

Your for (var matchId in matches) loop (a for...in loop ) is looping the property keys of the return object, which means the following keys are being looped ['matches', 'startIndex', 'endIndex', 'totalGames'] , ie 4 times. 您的for (var matchId in matches)循环(a for ... in循环 )正在循环返回对象的属性 ,这意味着以下键正在循环['matches', 'startIndex', 'endIndex', 'totalGames'] ,即4次。

And then the inner loop is repeating your return object's matches array (2 members), and thus 4*2 = 8 . 然后,内部循环重复您的返回对象的matches数组(2个成员),因此4*2 = 8

If you want to log the matchIds, what you want to do is perhaps this: 如果要记录matchIds,则可能需要执行以下操作:

lolapi.MatchList.getBySummonerId(obj['savisaar2'].id, options, function(err, matches) {
  var gameMatches = matches.matches;

  for(var i = 0; i < gameMatches.length; i++) {
    console.log(gameMatches[i].matchId);
  }
});

Or do it more functionally (does not work in Internet Explorer 8): 或更实用的方法(在Internet Explorer 8中不起作用):

lolapi.MatchList.getBySummonerId(obj['savisaar2'].id, options, function(err, matches) {
  matches.matches.forEach(function(gameMatch){
      console.log(gameMatch.matchId);
  });
  // Outputs:
  // 122934310
  // 122510663
});

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

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