在那里的理由使用array.forEach()以上为......的,当两个可以使用,在ES6? [英] Are there reasons to use array.forEach() over for...of, when both could be used, in ES6?

查看:240
本文介绍了在那里的理由使用array.forEach()以上为......的,当两个可以使用,在ES6?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 for..of 在2015年的ECMAScript(ES6)推出,是没有任何理由继续倡导使用年龄稍大的 Array.prototype.forEach()

很多今天写在ES6 code包含code,如:

 让总和= 0;
values​​.forEach((值)=> {
    总和+ =价值;
});

在几乎所有这些情况下,等效for..of环路可以用来代替

 让总和= 0;
对于(让值的值){
  总和+ =价值;
}

这两种格式的更简洁的版本:

  values​​.forEach(价值=>总结+ =值);
对于(让值的值)之和+ =价值;

该for..of格式的作品不仅与阵列对象,但其他iterables以及和更高性能。不像 Array.prototype.map()还有没有返回值 .forEach(),允许进一步链接。

有可以设想更容易阅读,所以在调用链(下面例子)的端部是有用的,或者如果每个项目应该应用一个未知的回调。这些边缘情况不谈,它应该无法避免?

  ['1','2',3] .MAP(数字)
             .forEach(价值=>总结+ =值)


解决方案

最重要的区别是,你不能没有扔从的forEach 破门,这是使用例外流量控制,因此不好的做法。

对于数组循环,将永远触摸每一个元素,的forEach ...是pretty等价的。然而,如果你有一个保证一度创出每一个元素的循环,你也许可以使用地图减少中地方的任何for-each结构,使事情更多功能。

您总和的例子是一个伟大的减少的forEach 是不是最好的选择,当你可以使用:

 让总和= values​​.reduce((总和值)=>总和+值,0);

如果你想挑选出的元素的数组,可以通常使用过滤器。对于流量控制,每个部分工作,并在必要时(如在评论中提到的)。反演

在一般情况下,的forEach 通常可以通过一个更具描述性和专门阵列的方法所取代,除非特别需要引起副作用。 是可迭代的对象非常有用,但并不总是最好的选择阵列。...

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

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

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

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;

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)

解决方案

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 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.

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);

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).

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.

这篇关于在那里的理由使用array.forEach()以上为......的,当两个可以使用,在ES6?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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