在 ES6 中,当两者都可以使用时,是否有理由使用 array.forEach() 而不是 for...of? [英] Are there reasons to use array.forEach() over for...of, when both could be used, in ES6?

查看:25
本文介绍了在 ES6 中,当两者都可以使用时,是否有理由使用 array.forEach() 而不是 for...of?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 for..of 在 ECMAScript 2015 (ES6) 中引入,是否有任何理由继续提倡使用稍旧的 Array.prototype.forEach()?

With for..of introduced in ECMAScript 2015 (ES6), is there any reason to continue advocating use of the slightly older Array.prototype.forEach()?

今天用 ES6 编写的很多代码都包含这样的代码:

A lot of code written in ES6 today contains code such as:

let sum = 0;
values.forEach((value) => {
    sum += value;
});

在几乎所有这些情况下,都可以使用等效的 for..of 循环:

In almost all of these situations, an equivalent for..of loop could be used instead:

let sum = 0;
for (let value of values) {
  sum += value;
}

两种格式的更简洁版本:

The more concise versions of the two formats:

values.forEach(value => sum += value);
for (let value of values) sum += value;

for..of 格式不仅适用于 Array 对象,也适用于其他可迭代对象,并且性能更高.与 Array.prototype.map() 不同,.forEach() 也没有允许进一步链接的返回值.

The for..of format works not only with Array objects but other iterables as well and is more performant. Unlike Array.prototype.map() there is also no return value to .forEach() that would allow for further chaining.

可以想象,它可能更具可读性,因此在调用链的末尾(示例如下)有用,或者如果每个项目都应应用于未知的回调.撇开那些边缘情况不谈,难道不应该避免吗?

It could conceivably be more readable and therefore useful at the end of a call chain (example below), or if each item should be applied to an unknown callback. Those edge cases aside, should it not be avoided?

['1', '2', 3].map(Number)
             .forEach(value => sum += value)

推荐答案

最重要的区别是你不能在不抛出的情况下从 forEach 中断,这是使用异常进行流控制,因此是不好的做法.

The most important difference is that you cannot break from a forEach without throwing, which is using exceptions for flow control and therefore bad practice.

对于一个总是接触每个元素的数组循环,for ... offorEach 是相当等效的.但是,如果您有一个保证每个元素命中一次的循环,您可能可以使用 mapreduce 代替任何 for-each 构造并使事情更具功能性.

For an array loop that will always touch every element, for ... of and forEach are pretty equivalent. However, if you have a loop that is guaranteed to hit every element once, you can probably use map or reduce in place of any for-each construct and make things More Functional.

您的 sum 示例非常适合 reduce.forEach 不是最好的选择,当你可以使用时:

Your sum example is a great one for reduce. forEach is not the best choice, when you can just use:

let sum = values.reduce((sum, value) => sum + value, 0);

如果要从数组中挑选元素,通常可以使用 filter.对于流控制,everysome 工作,必要时进行反转(如评论中所述).

If you want to pick elements out of an array, you can typically use filter. For flow control, every and some work, with inversion as necessary (as mentioned in the comments).

一般来说,forEach 通常可以替换为更具描述性和专门化的数组方法,除非您特别需要引起副作用.for...of 对于可迭代对象非常有用,但并不总是数组的最佳选择.

In general, forEach can usually be replaced by a more descriptive and specialized array method, unless you specifically need to cause side effects. for...of is very useful with iterable objects, but not always the best choice for arrays.

这篇关于在 ES6 中,当两者都可以使用时,是否有理由使用 array.forEach() 而不是 for...of?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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