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

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

这里我有一个典型的 for 循环,稍后将 i 作为参数传递 .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:遍历集合的元素,为每个元素调用 iteratee。 iteratee 绑定到 thisArg 并使用三个参数调用: (值、索引|键、集合)。 Iteratee 函数可以通过显式返回 false 提前退出迭代。

这意味着您可以像这样重构它:

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