简体   繁体   English

使用lodash我如何保持一个计数器,以后可以用作参数?

[英]Using lodash how I can keep a counter that later could used as a parameter?

here I have a typical for loop, where i is pass as a parameter later .splice() My question is: How I can refactor this with lodash? 在这里,我有一个典型的for循环,稍后我作为参数传递给我。splice()我的问题是:如何用lodash重构它?

        for(var i =0; i< scope.liveBalls.length;i++){
            if(scope.liveBalls[i].bat === scope.ball.bat){
                scope.splicedBalls.push(scope.liveBalls.splice(i,1));
            }
        }

_.each: Iterates over elements of collection invoking iteratee for each element. _.each:遍历集合元素,为每个元素调用iteratee。 The iteratee is bound to thisArg and invoked with three arguments: (value, index|key, collection). iteratee绑定到thisArg,并使用三个参数调用:(值,索引|键,集合)。 Iteratee functions may exit iteration early by explicitly returning false. Iteratee函数可以通过显式返回false来提前退出迭代。

This means that you can refactor it like this: 这意味着您可以像这样重构它:

_.each(scope.liveBalls, function (liveBall, index, liveBalls) {
  if(liveBall.bat === scope.ball.bat){
    scope.splicedBalls.push(liveBalls.splice(index, 1));
  }
});

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

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