Array中 forEach() 和 map() 的区别


本文摘自PHP中文网,作者青灯夜游,侵删。

相关推荐:《javascript视频教程》

今天我们来看一下 Array中 Array.forEach()Array.map()方法之间的区别。

forEach()map()方法通常用于遍历Array元素,但几乎没有区别,我们来一一介绍。

1.返回值

forEach()方法返回undefined ,而map()返回一个包含已转换元素的新数组。

1

2

3

4

5

6

7

8

9

10

11

const numbers = [1, 2, 3, 4, 5];

 

// 使用 forEach()

const squareUsingForEach = [];

numbers.forEach(x => squareUsingForEach.push(x*x));

 

// 使用 map()

const squareUsingMap = numbers.map(x => x*x);

 

console.log(squareUsingForEach); // [1, 4, 9, 16, 25]

console.log(squareUsingMap);     // [1, 4, 9, 16, 25]

由于forEach()返回undefined,所以我们需要传递一个空数组来创建一个新的转换后的数组。map()方法不存在这样的问题,它直接返回新的转换后的数组。在这种情况下,建议使用map()方法。

2.链接其他方法

map()方法输出可以与其他方法(如reduce()sort()filter())链接在一起,以便在一条语句中执行多个操作。

另一方面,forEach()是一个终端方法,这意味着它不能与其他方法链接,因为它返回undefined

我们使用以下两种方法找出数组中每个元素的平方和:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

onst numbers = [1, 2, 3, 4, 5];

 

// 使用 forEach()

const squareUsingForEach = []

let sumOfSquareUsingForEach = 0;

numbers.forEach(x => squareUsingForEach.push(x*x));

squareUsingForEach.forEach(square => sumOfSquareUsingForEach += square);

 

// 使用 map()

const sumOfSquareUsingMap = numbers.map(x => x*x).reduce((total, value) => total + value)

;

 

console.log(sumOfSquareUsingForEach); // 55

console.log(sumOfSquareUsingMap);     // 55

当需要多个操作时,使用forEach()方法是一项非常乏味的工作。我们可以在这种情况下使用map()方法。

3.性能

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

// Array:

var numbers = [];

for ( var i = 0; i < 1000000; i++ ) {

    numbers.push(Math.floor((Math.random() * 1000) + 1));

}

 

// 1. forEach()

console.time("forEach");

const squareUsingForEach = [];

numbers.forEach(x => squareUsingForEach.push(x*x));

console.timeEnd("forEach");

 

// 2. map()

console.time("map");

const squareUsingMap = numbers.map(x => x*x);

console.timeEnd("map");

这是在MacBook Pro的 Google Chrome v83.0.4103.106(64位)上运行上述代码后的结果。 建议复制上面的代码,然后在自己控制台中尝试一下。

1

2

forEach: 26.596923828125ms

map:     21.97998046875ms

显然,map()方法比forEach()转换元素要好。

4.中断遍历

这两种方法都不能用 break 中断,否则会引发异常:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

const numbers = [1, 2, 3, 4, 5];

 

// break; inside forEach()

const squareUsingForEach = [];

numbers.forEach(x => {

  if(x == 3) break; // <- SyntaxError

  squareUsingForEach.push(x*x);

});

 

// break; inside map()

const squareUsingMap = numbers.map(x => {

  if(x == 3) break; // <- SyntaxError

  return x*x;

});

上面代码会抛出 SyntaxError

1

? Uncaught SyntaxError: Illegal break statement

如果需要中断遍历,则应使用简单的for循环或for-of/for-in循环。

1

2

3

4

5

6

7

8

9

10

const numbers = [1, 2, 3, 4, 5];

 

// break; inside for-of loop

const squareUsingForEach = [];

for(x of numbers){

  if(x == 3) break;

  squareUsingForEach.push(x*x);

};

 

console.log(squareUsingForEach); // [1, 4]

5. 最后

建议使用map()转换数组的元素,因为它语法短,可链接且性能更好。

如果不想返回的数组或不转换数组的元素,则使用forEach() 方法。

最后,如果要基于某种条件停止或中断数组的便利,则应使用简单的for循环或for-of / for-in循环。

原文地址:https://codingnconcepts.com/javascript/difference-between-foreach-and-map-in-javascript-array/

作者:Ashish Lahoti

译文地址:https://segmentfault.com/a/1190000038421334

更多编程相关知识,请访问:编程教学!!

以上就是Array中 forEach() 和 map() 的区别的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

前端项目中目录结构优化的方法总结

javascript charat()方法是什么

javascript绑定事件的方法有哪些

java和javascript关系大吗

javascript怎么关闭窗口

asp与javascript的区别是什么

javascript如何删除所有的cookie

h5 canvas实现粒子时钟的详细方法

深入了解js中的for...of循环

javascript区不区分大小写

更多相关阅读请进入《javascript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...