如何提早打破reduce()方法? [英] How to early break reduce() method?

查看:65
本文介绍了如何提早打破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)

推荐答案

更新

一些注释者指出,要对原始数组进行突变,以便在.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](

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 = ['9', '91', '95', '96', '99'];
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, "\noriginal Arr: ", array);
// x:  99195
// original Arr:  [ '9', '91', '95', '96', '99' ]

OLD

您可以通过更改reduce函数的第4个参数"array"来中断.reduce()调用的任何迭代.无需自定义的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))

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

The 4th argument is the array being iterated over.

const array = ['9', '91', '95', '96', '99'];
const x = array
.reduce((acc, curr, i, arr) => {
    if(i === 2) arr.splice(1);  // eject early
    return acc += curr;
  }, '');
console.log('x: ', x);  // x:  99195

为什么?:

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

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天全站免登陆