简体   繁体   English

JavaScript:.forEach() 和.map() 之间的区别

[英]JavaScript: Difference between .forEach() and .map()

I know that there were a lot of topics like this.我知道有很多这样的话题。 And I know the basics: .forEach() operates on original array and .map() on the new one.而且我知道基础知识: .forEach()对原始数组进行操作, .map()在新数组上操作。

In my case:就我而言:

function practice (i){
    return i+1;
};

var a = [ -1, 0, 1, 2, 3, 4, 5 ];
var b = [ 0 ];
var c = [ 0 ];
console.log(a);
b = a.forEach(practice);
console.log("=====");
console.log(a);
console.log(b);
c = a.map(practice);
console.log("=====");
console.log(a);
console.log(c);

And this is output:这是 output:

[ -1, 0, 1, 2, 3, 4, 5 ]
=====
[ -1, 0, 1, 2, 3, 4, 5 ]
undefined
=====
[ -1, 0, 1, 2, 3, 4, 5 ]
[ 0, 1, 2, 3, 4, 5, 6 ]

I can't understand why using practice changes value of b to undefined .我不明白为什么使用practiceb的值更改为undefined
I'm sorry if this is silly question, but I'm quite new in this language and answers I found so far didn't satisfy me.如果这是一个愚蠢的问题,我很抱歉,但我对这种语言很陌生,到目前为止我发现的答案并不能让我满意。

They are not one and the same.它们不是一回事。 Let me explain the difference.让我解释一下区别。

forEach : This iterates over a list and applies some operation with side effects to each list member (example: saving every list item to the database) and does not return anything. forEach :这会遍历一个列表并对每个列表成员应用一些具有副作用的操作(例如:将每个列表项保存到数据库中)并且不返回任何内容。

map : This iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (example: transforming list of strings to uppercase). map :这会遍历一个列表,转换该列表的每个成员,并返回另一个与转换后的成员大小相同的列表(例如:将字符串列表转换为大写)。 It does not mutate the array on which it is called (although the callback function may do so).它不会改变调用它的数组(尽管回调函数可能会这样做)。

References参考

Array.prototype.forEach() - JavaScript | Array.prototype.forEach() - JavaScript | MDN MDN

Array.prototype.map() - JavaScript | Array.prototype.map() - JavaScript | MDN MDN

  • Array.forEach “executes a provided function once per array element.” Array.forEach “每个数组元素执行一次提供的函数。”

  • Array.map “creates a new array with the results of calling a provided function on every element in this array.” Array.map “创建一个新数组,其结果是对该数组中的每个元素调用提供的函数。”

So, forEach doesn't actually return anything.所以, forEach实际上并没有返回任何东西。 It just calls the function for each array element and then it's done.它只是为每个数组元素调用函数,然后就完成了。 So whatever you return within that called function is simply discarded.因此,您在该被调用函数中返回的任何内容都将被丢弃。

On the other hand, map will similarly call the function for each array element but instead of discarding its return value, it will capture it and build a new array of those return values.另一方面, map将类似地为每个数组元素调用该函数,但不是丢弃它的返回值,而是捕获它并构建一个包含这些返回值的新数组。

This also means that you could use map wherever you are using forEach but you still shouldn't do that so you don't collect the return values without any purpose.这也意味着您可以在使用forEach的任何地方使用map ,但您仍然不应该这样做,因此您不会无缘无故地收集返回值。 It's just more efficient to not collect them if you don't need them.如果您不需要它们,则不收集它们会更有效。

forEach() forEach() map()地图()
Functionality功能性 Performs given operation on each element of the array对数组的每个元素执行给定的操作 Performs given "transformation" on a "copy" of each element对每个元素的“副本”执行给定的“转换”
Return value返回值 Returns undefined返回undefined Returns new array with transformed elements, leaving back original array unchanged.返回具有转换元素的新数组,保持原始数组不变。
Preferrable usage scenario and example优选使用场景及示例 Performing non-tranformation like processing on each element.对每个元素执行非转换之类的处理。

For example, saving all elements in the database.例如,保存数据库中的所有元素。
Obtaining array containing output of some processing done on each element of the array.获取包含对数组的每个元素进行的某些处理的输出的数组。

For example, obtaining array of lengths of each string in the array例如,获取数组中每个字符串的长度数组

forEach() example forEach()示例

 chars = ['Hello' , 'world!!!'] ; var retVal = chars.forEach(function(word){ console.log("Saving to db: " + word) }) console.log(retVal) //undefined

map() example map()示例

 chars = ['Hello' , 'world!!!'] ; var lengths = chars.map(function(word){ return word.length }) console.log(lengths) //[5,8]

The main difference that you need to know is .map() returns a new array while .forEach() doesn't.您需要知道的主要区别是.map()返回一个新数组,而.forEach()不返回。 That is why you see that difference in the output.这就是为什么您会在输出中看到这种差异。 .forEach() just operates on every value in the array. .forEach()只对数组中的每个值进行操作。

Read up:阅读:

You might also want to check out: - Array.prototype.every() - JavaScript |您可能还想查看: - Array.prototype.every() - JavaScript | MDN MDN

Performance Analysis For loops performs faster than map or foreach as number of elements in a array increases.性能分析随着数组中元素数量的增加,For 循环的执行速度比 map 或 foreach 快。

 let array = []; for (var i = 0; i < 20000000; i++) { array.push(i) } console.time('map'); array.map(num => { return num * 4; }); console.timeEnd('map'); console.time('forEach'); array.forEach((num, index) => { return array[index] = num * 4; }); console.timeEnd('forEach'); console.time('for'); for (i = 0; i < array.length; i++) { array[i] = array[i] * 2; } console.timeEnd('for');

forEach: If you want to perform an action on the elements of an Array and it is same as you use for loop. forEach:如果要对 Array 的元素执行操作,它与使用 for 循环相同。 The result of this method does not give us an output buy just loop through the elements.这种方法的结果并没有给我们一个输出,只是循环遍历元素。

map: If you want to perform an action on the elements of an array and also you want to store the output of your action into an Array. map:如果您想对数组的元素执行操作,并且还想将操作的输出存储到数组中。 This is similar to for loop within a function that returns the result after each iteration.这类似于在每次迭代后返回结果的函数中的 for 循环。

Hope this helps.希望这可以帮助。

map returns a new array. map返回一个新数组。

forEach has no return value. forEach没有返回值。

That's the heart of the difference.这就是差异的核心。 Most of the other answers here say effectively that, but in a much more convoluted way.这里的大多数其他答案都有效地说明了这一点,但方式更加复杂。

The difference lies in what they return.不同之处在于它们返回的内容。 After execution:执行后:

arr.map()

returns an array of elements resulting from the processed function;返回处理后的函数产生的元素数组; while:尽管:

arr.forEach()

returns undefined.返回未定义。

forEach() : forEach()

return value : undefined返回值:未定义

originalArray : not modified after the method call originalArray : 方法调用后未修改

newArray is not created after the end of method call.方法调用结束后不会创建 newArray。

map() :地图()

return value : new Array populated with the results of calling a provided function on every element in the calling array返回值:新数组填充了在调用数组中的每个元素上调用提供的函数的结果

originalArray : not modified after the method call originalArray : 方法调用后未修改

newArray is created after the end of method call. newArray 在方法调用结束后创建。

Since map builds a new array, using it when you aren't using the returned array is an anti-pattern;由于 map 构建了一个新数组,因此在不使用返回的数组时使用它是一种反模式; use forEach or for-of instead.使用 forEach 或 for-of 代替。

Diffrence between Foreach & map : Foreach 和 map 的区别:

Map() : If you use map then map can return new array by iterating main array. Map() :如果你使用 map 那么 map 可以通过迭代主数组来返回新数组。

Foreach() : If you use Foreach then it can not return anything for each can iterating main array. Foreach() :如果你使用 Foreach 那么它不能为每个可以迭代的主数组返回任何东西。

useFul link : use this link for understanding diffrence useFul 链接:使用此链接了解差异

https://codeburst.io/javascript-map-vs-foreach-f38111822c0f https://codeburst.io/javascript-map-vs-foreach-f38111822c0f

Difference between forEach() & map() forEach() 和 map() 的区别

forEach() just loop through the elements. forEach()只是循环遍历元素。 It's throws away return values and always returns undefined.The result of this method does not give us an output .它会丢弃返回值并始终返回 undefined。此方法的结果不会给我们输出。

map() loop through the elements allocates memory and stores return values by iterating main array map()循环遍历元素分配内存并通过迭代主数组存储返回值

Example:例子:

   var numbers = [2,3,5,7];

   var forEachNum = numbers.forEach(function(number){
      return number
   })
   console.log(forEachNum)
   //output undefined

   var mapNum = numbers.map(function(number){
      return number
   })
   console.log(mapNum)
   //output [2,3,5,7]

map() is faster than forEach() map() 比 forEach() 快

one of the shuttle difference not mentioned here is that forEach() can loop over static (not live) NodeList while map() cannot这里没有提到的穿梭差异之一是forEach()可以循环静态(非实时)NodeList 而map()不能

//works perfectly
      document.querySelectorAll('.score').forEach(element=>console.log(element));
    
    
  //Uncaught TypeError: document.querySelectorAll(...).map is not a function        
      document.querySelectorAll('.score').map(element=>console.log(element));

One thing to point out is that both methods skips uninitialized values, but map keeps them in the returned array.需要指出的一件事是,这两种方法都会跳过未初始化的值,但 map 将它们保留在返回的数组中。

var arr = [1, , 3];

arr.forEach(function(element) {
    console.log(element);
});
//Expected output: 1 3

console.log(arr.map(element => element));
//Expected output: [1, undefined, 3];

The main difference between forEach and map method is that forEach method just loop through the array and doesn't return anything. forEach 和 map 方法的主要区别在于 forEach 方法只是循环遍历数组并且不返回任何内容。

let arr = [1,2,3,4];
let newArr = arr.forEach((x) => x);
console.log(newArr); // prints undefined

While, map returns a new array.同时,map 返回一个新数组。

let arr = [1,2,3,4];
let newArr = arr.map((x) => x);
console.log(newArray); // [1,2,3,4]

We can use map to create a deep copy of an array.我们可以使用 map 来创建数组的深拷贝。

在此处输入图像描述

const arr = [...Array(100000000).keys()];

console.time("for");
for (let i = 0; i < arr.length; i++) {}
console.timeEnd("for");

console.time("while");
let j = 0;
while (j < arr.length) {
  j++;
}
console.timeEnd("while");

console.time("dowhile");
let k = 0;
do {
  k++;
} while (k < arr.length);
console.timeEnd("dowhile");

console.time("forEach");
arr.forEach((element) => {});
console.timeEnd("forEach");

VM35:6 for: 45.998046875 ms VM35:6 为:45.998046875 毫秒

VM35:13 while: 154.581787109375 ms VM35:13 而:154.581787109375 毫秒

VM35:20 dowhile: 141.97216796875 ms VM35:20 时长:141.97216796875 毫秒

VM35:24 forEach: 776.469970703125 ms VM35:24 forEach:776.469970703125 毫秒

Performance Analysis (again - not very scientific)性能分析(再次——不是很科学)
In my experience sometime .map() can be faster than .foreach()根据我的经验,有时 .map() 可能比 .foreach() 更快

 let rows = []; for (let i = 0; i < 10000000; i++) { // console.log("here", i) rows.push({ id: i, title: 'ciao' }); } const now1 = Date.now(); rows.forEach(row => { if (!row.event_title) { row.event_title = `no title ${row.event_type}`; } }); const now2 = Date.now(); rows = rows.map(row => { if (!row.event_title) { row.event_title = `no title ${row.event_type}`; } return row; }); const now3 = Date.now(); const time1 = now2 - now1; const time2 = now3 - now2; console.log('forEach time', time1); console.log('.map time', time2);

On my macbook pro (late 2013)在我的 macbook pro 上(2013 年末)

  • forEach time 1909每次 1909
  • .map time 444 .map 时间 444

.map and .forEach will do just about then same thing, until you start operating on arrays with millions of elements. .map 和 .forEach 将做同样的事情,直到您开始对具有数百万个元素的数组进行操作。 .map will create another collection with the same size (and possibly type, depending on the array species) which could use up a LOT of memory. .map 将创建另一个具有相同大小(可能还有类型,取决于数组种类)的集合,这可能会占用大量内存。 .forEach will not do this. .forEach 不会这样做。

Map implicitly returns while forEach does not. Map 隐式返回,而 forEach 没有。

This is why when you're coding a JSX application, you almost always use map instead of forEach to display content in React .这就是为什么在编写 JSX 应用程序时,几乎总是使用 map 而不是forEach 在 React 中显示内容

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

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