您好,登錄后才能下訂單哦!
本篇文章為大家展示了JavaScript中forEach() 和 map() 的區(qū)別是什么,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
1.返回值
forEach()方法返回undefined ,而map()返回一個(gè)包含已轉(zhuǎn)換元素的新數(shù)組。
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,所以我們需要傳遞一個(gè)空數(shù)組來(lái)創(chuàng)建一個(gè)新的轉(zhuǎn)換后的數(shù)組。map()方法不存在這樣的問(wèn)題,它直接返回新的轉(zhuǎn)換后的數(shù)組。在這種情況下,建議使用map()方法。
2.鏈接其他方法
map()方法輸出可以與其他方法(如reduce()、sort()、filter())鏈接在一起,以便在一條語(yǔ)句中執(zhí)行多個(gè)操作。
另一方面,forEach()是一個(gè)終端方法,這意味著它不能與其他方法鏈接,因?yàn)樗祷豼ndefined。
我們使用以下兩種方法找出數(shù)組中每個(gè)元素的平方和:
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
當(dāng)需要多個(gè)操作時(shí),使用forEach()方法是一項(xiàng)非常乏味的工作。我們可以在這種情況下使用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位)**上運(yùn)行上述代碼后的結(jié)果。建議復(fù)制上面的代碼,然后在自己控制臺(tái)中嘗試一下。
forEach: 26.596923828125ms map: 21.97998046875ms
顯然,map()方法比f(wàn)orEach()轉(zhuǎn)換元素要好。
4.中斷遍歷
這兩種方法都不能用 break 中斷,否則會(huì)引發(fā)異常:
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; });
上面代碼會(huì)拋出 SyntaxError:
? Uncaught SyntaxError: Illegal break statement
如果需要中斷遍歷,則應(yīng)使用簡(jiǎn)單的for循環(huán)或for-of/for-in循環(huán)。
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()轉(zhuǎn)換數(shù)組的元素,因?yàn)樗Z(yǔ)法短,可鏈接且性能更好。
如果不想返回的數(shù)組或不轉(zhuǎn)換數(shù)組的元素,則使用forEach() 方法。
上述內(nèi)容就是JavaScript中forEach() 和 map() 的區(qū)別是什么,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。