日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JS数组中 forEach() 和 map() 的区别

發布時間:2025/3/21 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JS数组中 forEach() 和 map() 的区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JS數組中 forEach() 和 map() 的區別

Array中 Array.forEach()和 Array.map()方法之間的區別。

forEach()和map()方法通常用于遍歷Array元素,但幾乎沒有區別

1、返回值

forEach()方法返回undefined ,而map()返回一個包含已轉換元素的新數組。

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。

我們使用以下兩種方法找出數組中每個元素的平方和:

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、性能

// 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位)上運行上述代碼后的結果。建議復制上面的代碼,然后在自己控制臺中嘗試一下。

forEach: 26.596923828125ms map: 21.97998046875ms

顯然,map()方法比forEach()轉換元素要好。

4、中斷遍歷

這兩種方法都不能用 break 中斷,否則會引發異常:

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:

? Uncaught SyntaxError: Illegal break statement

如果需要中斷遍歷,則應使用簡單的for循環或for-of/for-in循環。

const numbers = [1, 2, 3, 4, 5];//break;insidefor-ofloop 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循環。

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的JS数组中 forEach() 和 map() 的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。