简体   繁体   English

Array.protoype。每一个都比Array.prototype.forEach有什么劣势?

[英]Disadvantages of Array.protoype.every over Array.prototype.forEach?

Both methods iterate over the current array's elements but only Array.prototype.every allows you to break out of the loop similar to how you can use break to exit a for-loop. 两种方法都迭代当前数组的元素,但是只有Array.prototype.every允许您跳出循环,类似于使用break退出for循环的方式。

When looking at browser compatibility matrixes both seem to be supported by the same browsers. 当查看浏览器兼容性矩阵时 ,似乎两个浏览器都支持这两个矩阵

Are there any disadvantages (performance, readability, ...) to Array.prototype.every compared to forEach that I have missed or why isn't every more common? 与我错过的forEach相比, Array.prototype.every有什么缺点(性能,可读性...),或者为什么every都不常见?

Main main point here is that most of the times I want to use forEach as a replacement for the classic for-loop for the sake of readability, but given that there is no way to break out of a for-each loop, every appears to be more more an equivalent here. 这里的主要要点是,出于可读性考虑,大多数时候我都想用forEach代替经典的for循环,但是鉴于没有办法打破for-each循环,每次都似乎在这里更等效。 The option to use every for that has been mentioned on MDN . MDN上已经提到了使用every的选项。

They have very different purposes. 他们有不同的目的。 While forEach executes a function with side effects (not very functional) for each element and returns nothing, every is a functional method to test whether all elements fulfill a given predicate and returns a boolean. 尽管forEach为每个元素执行一个具有副作用(不是很功能)的函数,但不返回任何内容,但是every方法都是一种功能性方法,用于测试所有元素是否都满足给定的谓词并返回布尔值。 Also, every does expect a callback that returns a boolean - which most forEach callbacks dont. 而且, every都希望有一个返回boolean的回调-大多数forEach回调every不会。

As an example: 举个例子:

> [1,2,3].forEach(console.log)
1
2
3
undefined
> [1,2,3].every(console.log)
1
false

If you want to use the iteration methods and need to break out, then yes you might abuse some / every , but that's not what they're made for. 如果您想使用迭代方法并且需要突破,那么是的,您可能会滥用some / every ,但这并不是它们的目的。 Notice that breaking the loop by returning a boolean does not lead to much better readability :-) I'd suggest to use them only when you can make use of the return value, ie if you need to know whether the loop aborted prematurely. 请注意,通过返回布尔值来中断循环不会带来更好的可读性:-)我建议仅在可以利用返回值的情况下(即,如果您需要知道循环是否过早中止)才使用它们。

The purpose of every is to check if every element of an array meets a test - and it returns true of it is so. every的目的是检查数组的每个元素是否都符合测试-并且它返回true。 forEach just runs its argument for each element of the array. forEach仅对数组的每个元素运行其参数。 So in a sense if your function returns true, every and forEach are equivalent. 因此,从某种意义上说,如果您的函数返回true,则Every和forEach是等效的。 But it is better to be explicit - if you want to test all elements, use every. 但是最好是明确的-如果要测试所有元素,请使用每个元素。 If you want to execute a function for each element, use forEach. 如果要为每个元素执行一个函数,请使用forEach。

From this exact question here: map, foreach and every 从这里的确切问题开始: 地图,foreach和每个

The difference is in the return values. 区别在于返回值。

.map() returns a new Array of objects created by taking some action on the original item. .map() 返回通过对原始项目执行某些操作而创建的新对象数组

.every() returns a boolean - true if every element in this array satisfies the provided testing function. 如果此数组中的每个元素都满足提供的测试功能,则.every() 返回boolean -true。 An important difference with .every() is that the test function may not always be called for every element in the array. .every()一个重要区别是,可能不一定总是为数组中的每个元素都调用测试函数。 Once the testing function returns false for any element, no more array elements are iterated. 一旦测试函数对任何元素返回false,就不再迭代数组元素。 Therefore, the testing function should usually have no side effects . 因此,测试功能通常应该没有副作用

.forEach() returns nothing - It iterates the Array performing a given action for each item in the Array. .forEach() 返回任何内容 -迭代Array,对Array中的每个项目执行给定的操作。

Edit: Here's the MSDN Docs if you prefer. 编辑:如果您愿意,这是MSDN文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM