简体   繁体   English

.map、.every 和 .forEach 之间有什么区别?

[英]What is the difference between .map, .every, and .forEach?

I've always wondered what the difference between them were.我一直想知道他们之间的区别是什么。 They all seem to do the same thing...他们似乎都在做同样的事情......

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()返回一个布尔值- 如果此数组中的每个元素都满足提供的测试函数,则返回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()返回任何内容- 它迭代数组,为数组中的每个项目执行给定的操作。

Read about these and the many other Array iteration methods at MDN . 在 MDN阅读这些和许多其他数组迭代方法

gilly3's answer is great. gilly3 的回答很棒。 I just wanted to add a bit of information about other types of "loop through elements" functions.我只是想添加一些关于其他类型的“循环元素”函数的信息。

  • .every() (stops looping the first time the iterator returns false or something falsey) .every() (在迭代器第一次返回 false 或错误时停止循环)
  • .some() (stops looping the first time the iterator returns true or something truthy) .some() (当迭代器第一次返回真值或某些真值时停止循环)
  • .filter() (creates a new array including elements where the filter function returns true and omitting the ones where it returns false) .filter() (创建一个新数组,其中包含过滤器函数返回 true 的元素并忽略它返回 false 的元素)
  • .map() (creates a new array from the values returned by the iterator function) .map() (根据迭代器函数返回的值创建一个新数组)
  • .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) .reduce() (通过重复调用迭代器来构建一个值,传入以前的值;查看规范以获取详细信息;用于对数组的内容和许多其他内容求和)
  • .reduceRight() (like reduce, but works in descending rather than ascending order) .reduceRight() (类似于reduce,但按降序而不是升序工作)

credit to: TJCrowder For-each over an array in JavaScript?归功于:TJCrowder For-each 在 JavaScript 中的数组?

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(),你会得到一个错误。

For Ramda , the difference between R.map() and R.forEach() is:对于Ramda ,之间的差R.map()R.forEach()为:

  1. R.forEach() returns the original array whereas R.map() returns a functor R.forEach()返回原始数组而R.map()返回一个函子
  2. R.forEach() can only operate on array but R.map() can also operate on an object (ie the key/value pairs of the object are treated like an array) R.forEach()只能对数组进行操作,而R.map()也可以对对象进行操作(即对象的键/值对被视为数组)

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

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