自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JS數(shù)組中 forEach() 和 map() 的區(qū)別

開發(fā) 前端
今天我們來看一下 Array中 Array.forEach()和 Array.map()方法之間的區(qū)別。

[[359324]]

 今天我們來看一下 Array中 Array.forEach()和 Array.map()方法之間的區(qū)別。

forEach()和map()方法通常用于遍歷Array元素,但幾乎沒有區(qū)別,我們來一一介紹。

1.返回值

forEach()方法返回undefined ,而map()返回一個(gè)包含已轉(zhuǎn)換元素的新數(shù)組。

  1. const numbers = [1, 2, 3, 4, 5]; 
  2.  
  3. // 使用 forEach() 
  4. const squareUsingForEach = []; 
  5. numbers.forEach(x => squareUsingForEach.push(x*x)); 
  6.  
  7. // 使用 map() 
  8. const squareUsingMap = numbers.map(x => x*x); 
  9.  
  10. console.log(squareUsingForEach); // [1, 4, 9, 16, 25] 
  11. console.log(squareUsingMap);     // [1, 4, 9, 16, 25] 

 由于forEach()返回undefined,所以我們需要傳遞一個(gè)空數(shù)組來創(chuàng)建一個(gè)新的轉(zhuǎn)換后的數(shù)組。map()方法不存在這樣的問題,它直接返回新的轉(zhuǎn)換后的數(shù)組。在這種情況下,建議使用map()方法。

2.鏈接其他方法

map()方法輸出可以與其他方法(如reduce()、sort()、filter())鏈接在一起,以便在一條語句中執(zhí)行多個(gè)操作。

另一方面,forEach()是一個(gè)終端方法,這意味著它不能與其他方法鏈接,因?yàn)樗祷豼ndefined。

我們使用以下兩種方法找出數(shù)組中每個(gè)元素的平方和:

  1. onst numbers = [1, 2, 3, 4, 5]; 
  2.  
  3. // 使用 forEach() 
  4. const squareUsingForEach = [] 
  5. let sumOfSquareUsingForEach = 0; 
  6. numbers.forEach(x => squareUsingForEach.push(x*x)); 
  7. squareUsingForEach.forEach(square => sumOfSquareUsingForEach += square); 
  8.  
  9. // 使用 map() 
  10. const sumOfSquareUsingMap = numbers.map(x => x*x).reduce((total, value) => total + value) 
  11.  
  12. console.log(sumOfSquareUsingForEach); // 55 
  13. console.log(sumOfSquareUsingMap);     // 55 

 當(dāng)需要多個(gè)操作時(shí),使用forEach()方法是一項(xiàng)非常乏味的工作。我們可以在這種情況下使用map()方法。

3.性能

  1. // Array: 
  2. var numbers = []; 
  3. for ( var i = 0; i < 1000000; i++ ) { 
  4.     numbers.push(Math.floor((Math.random() * 1000) + 1)); 
  5.  
  6. // 1. forEach() 
  7. console.time("forEach"); 
  8. const squareUsingForEach = []; 
  9. numbers.forEach(x => squareUsingForEach.push(x*x)); 
  10. console.timeEnd("forEach"); 
  11.  
  12. // 2. map() 
  13. console.time("map"); 
  14. const squareUsingMap = numbers.map(x => x*x); 
  15. console.timeEnd("map"); 

 這是在MacBook Pro的 **Google Chrome v83.0.4103.106(64位)**上運(yùn)行上述代碼后的結(jié)果。建議復(fù)制上面的代碼,然后在自己控制臺(tái)中嘗試一下。

  1. forEach: 26.596923828125ms 
  2. map:     21.97998046875ms 

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

4.中斷遍歷

這兩種方法都不能用 break 中斷,否則會(huì)引發(fā)異常:

  1. const numbers = [1, 2, 3, 4, 5]; 
  2.  
  3. // break; inside forEach() 
  4. const squareUsingForEach = []; 
  5. numbers.forEach(x => {  
  6.   if(x == 3) break; // <- SyntaxError  
  7.   squareUsingForEach.push(x*x); 
  8. }); 
  9.  
  10. // break; inside map() 
  11. const squareUsingMap = numbers.map(x => { 
  12.   if(x == 3) break; // <- SyntaxError  
  13.   return x*x; 
  14. }); 

 上面代碼會(huì)拋出 SyntaxError:

  1. ⓧ Uncaught SyntaxError: Illegal break statement 

如果需要中斷遍歷,則應(yīng)使用簡(jiǎn)單的for循環(huán)或for-of/for-in循環(huán)。

  1. const numbers = [1, 2, 3, 4, 5]; 
  2.  
  3. // break; inside for-of loop 
  4. const squareUsingForEach = []; 
  5. for(x of numbers){ 
  6.   if(x == 3) break; 
  7.   squareUsingForEach.push(x*x); 
  8. }; 
  9.  
  10. console.log(squareUsingForEach); // [1, 4] 

 5. 最后

建議使用map()轉(zhuǎn)換數(shù)組的元素,因?yàn)樗Z法短,可鏈接且性能更好。

如果不想返回的數(shù)組或不轉(zhuǎn)換數(shù)組的元素,則使用forEach() 方法。

最后,如果要基于某種條件停止或中斷數(shù)組的便利,則應(yīng)使用簡(jiǎn)單的for循環(huán)或for-of /for-in循環(huán)。

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2020-08-02 23:20:36

JavaScriptmap()forEach()

2023-06-14 08:54:09

Map方法ForEach方法

2024-03-11 01:00:00

jsfor循環(huán)

2022-10-12 14:39:27

Streammappeek

2024-08-30 08:43:24

JavaScriptforEachfor循環(huán)

2021-08-17 11:14:49

VoidJSTS

2022-09-07 11:52:48

forforEach前端

2023-05-11 07:41:03

Java 8tMap方法

2013-09-11 09:49:18

Java數(shù)組集合

2023-03-29 08:03:53

2022-08-27 14:42:45

Java集合數(shù)組

2009-08-28 17:18:55

foreach循環(huán)

2022-06-01 08:12:32

JS類數(shù)組對(duì)象

2009-06-25 15:20:28

CollectionMap

2021-03-29 12:01:00

遍歷數(shù)組for循環(huán)

2022-10-27 19:32:20

切片golang數(shù)組

2023-10-12 08:25:18

Javaequals內(nèi)存

2021-08-04 08:33:59

TypeScriptConst Readonly

2023-10-27 15:31:04

For循環(huán)Foreach循環(huán)

2021-02-22 08:19:49

js前端元素
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)