Javascript .map、.every 和 .forEach 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7340893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between .map, .every, and .forEach?
提问by 0x499602D2
I've always wondered what the difference between them were. They all seem to do the same thing...
我一直想知道他们之间的区别是什么。他们似乎都在做同样的事情......
回答by gilly3
The difference is in the return values.
区别在于返回值。
.map()
returns a new Array of objectscreated 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. An important difference with .every()
is that the test function may not always be called for every element in the array. Once the testing function returns false for any element, no more array elements are iterated. Therefore, the testing function should usually have no side effects.
.every()
返回一个布尔值- 如果此数组中的每个元素都满足提供的测试函数,则为 true。与 的一个重要区别.every()
是测试函数可能并不总是为数组中的每个元素调用。一旦测试函数对任何元素返回 false,就不再迭代数组元素。因此,测试功能通常应该没有副作用。
.forEach()
returns nothing- It iterates the Array performing a given action for each item in the Array.
.forEach()
什么都不返回- 它迭代数组,为数组中的每个项目执行给定的操作。
Read about these and the many other Array iteration methods at MDN.
回答by raphaelgontijolopes
gilly3's answer is great. I just wanted to add a bit of information about other types of "loop through elements" functions.
gilly3 的回答很棒。我只是想添加一些关于其他类型的“循环元素”函数的信息。
.every()
(stops looping the first time the iterator returns false or something falsey).some()
(stops looping the first time the iterator returns true or something truthy).filter()
(creates a new array including elements where the filter function returns true and omitting the ones where it returns false).map()
(creates a new array from the values returned by the iterator function).reduce()
(builds up a value by repeated calling the iterator, passing in previous values; see the spec for the details; useful for summing the contents of an array and many other things).reduceRight()
(like reduce, but works in descending rather than ascending order)
.every()
(在迭代器第一次返回 false 或错误时停止循环).some()
(当迭代器第一次返回真值或真值时停止循环).filter()
(创建一个新数组,其中包含过滤器函数返回 true 的元素并省略它返回 false 的元素).map()
(根据迭代器函数返回的值创建一个新数组).reduce()
(通过重复调用迭代器来建立一个值,传入以前的值;有关详细信息,请参阅规范;对汇总数组的内容和许多其他内容很有用).reduceRight()
(像reduce,但按降序而不是升序工作)
credit to: T.J.Crowder For-each over an array in JavaScript?
归功于:TJCrowder For-each 在 JavaScript 中的数组?
回答by Nadine Rose
Another consideration to the above great answers is chaining. With forEach() you can't chain, but with map(), you can.
上述优秀答案的另一个考虑因素是链接。使用 forEach() 不能链接,但使用 map() 可以。
For example:
例如:
var arrayNumbers = [3,1,2,4,5];
arrayNumbers.map(function(i) {
return i * 2
}).sort();
with .forEach(), you can't do the .sort(), you'll get an error.
使用 .forEach(),你不能做 .sort(),你会得到一个错误。
回答by Michael Osofsky
For Ramda, the difference between R.map()
and R.forEach()
is:
对于Ramda,之间的区别R.map()
,并R.forEach()
为:
R.forEach()
returns the original array whereasR.map()
returns a functorR.forEach()
can only operate on array butR.map()
can also operate on an object (i.e. the key/value pairs of the object are treated like an array)
R.forEach()
返回原始数组而R.map()
返回一个函子R.forEach()
只能对数组进行操作,R.map()
也可以对对象进行操作(即对象的键值对被当作数组处理)