如何提前中断reduce()方法? [英] How to early break reduce() method?

查看:79
本文介绍了如何提前中断reduce()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何中断reduce()方法的迭代?

for:

for (var i = Things.length - 1; i >= 0; i--) {
  if(Things[i] <= 0){
    break;
  }
};

reduce()

Things.reduce(function(memo, current){
  if(current <= 0){
    //break ???
    //return; <-- this will return undefined to memo, which is not what I want
  }
}, 0)

推荐答案

UPDATE

一些评论者提出了一个很好的观点,即原始数组正在发生变异,以便尽早破坏 .reduce() 逻辑.

Some of the commentators make a good point that the original array is being mutated in order to break early inside the .reduce() logic.

因此,我在调用后续 .reduce().slice(0) 修改了答案轻微> 步骤,生成原始数组的副本.注意:完成相同任务的类似操作是 slice()(不太明确)和扩展运算符 [...array](性能稍差).请记住,所有这些都为整体运行时间增加了一个额外的线性时间常数因子 + 1*(O(1)).

Therefore, I've modified the answer slightly by adding a .slice(0) before calling a follow-on .reduce() step, yielding a copy of the original array. NOTE: Similar ops that accomplish the same task are slice() (less explicit), and spread operator [...array] (slightly less performant). Bear in mind, all of these add an additional constant factor of linear time to the overall runtime + 1*(O(1)).

副本,用于保护原始数组免受最终导致迭代弹出的突变.

The copy, serves to preserve the original array from the eventual mutation that causes ejection from iteration.

const array = ['apple', '-pen', '-pineapple', '-pen'];
const x = array
    .slice(0)                         // create copy of "array" for iterating
    .reduce((acc, curr, i, arr) => {
       if (i === 2) arr.splice(1);    // eject early by mutating iterated copy
       return (acc += curr);
    }, '');

console.log("x: ", x, "
original Arr: ", array);
// x:  apple-pen-pineapple
// original Arr:  ['apple', '-pen', '-pineapple', '-pen']

您可以通过改变 reduce 函数的第四个参数array"来中断 .reduce() 调用的任何迭代.不需要自定义减少功能.请参阅文档,了解.reduce() 参数.

You CAN break on any iteration of a .reduce() invocation by mutating the 4th argument of the reduce function: "array". No need for a custom reduce function. See Docs for full list of .reduce() parameters.

Array.prototype.reduce((acc, curr, i, array))

第四个参数是被迭代的数组.

The 4th argument is the array being iterated over.

const array = ['apple', '-pen', '-pineapple', '-pen'];
const x = array
.reduce((acc, curr, i, arr) => {
    if(i === 2) arr.splice(1);  // eject early
    return acc += curr;
  }, '');
console.log('x: ', x);  // x:  apple-pen-pineapple

为什么?:

我能想到使用它而不是提供的许多其他解决方案的一个也是唯一的原因是,如果您想为您的算法维护一种函数式编程方法,并且您希望使用最具声明性的方法来实现这一点.如果您的整个目标是将数组从字面上减少为替代的非虚假原语(字符串、数字、布尔值、符号),那么我认为这实际上是最好的方法.

The one and only reason I can think of to use this instead of the many other solutions presented is if you want to maintain a functional programming methodology to your algorithm, and you want the most declarative approach possible to accomplish that. If your entire goal is to literally REDUCE an array to an alternate non-falsey primitive (string, number, boolean, Symbol) then I would argue this IS in fact, the best approach.

为什么不呢?

有一个完整的参数列表来防止不改变函数参数,因为这是一种不好的做法.

There's a whole list of arguments to make for NOT mutating function parameters as it's a bad practice.

这篇关于如何提前中断reduce()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆