Spread Syntax (...) - JavaScript - MDN Web Docs - Mozilla
Có thể bạn quan tâm
A more powerful array literal
Without spread syntax, the array literal syntax is no longer sufficient to create a new array using an existing array as one part of it. Instead, imperative code must be used using a combination of methods, including push(), splice(), concat(), etc. With spread syntax, this becomes much more succinct:
jsconst parts = ["shoulders", "knees"]; const lyrics = ["head", ...parts, "and", "toes"]; // ["head", "shoulders", "knees", "and", "toes"]Just like spread for argument lists, ... can be used anywhere in the array literal, and may be used more than once.
Copying an array
You can use spread syntax to make a shallow copy of an array. Each array element retains its identity without getting copied.
jsconst arr = [1, 2, 3]; const arr2 = [...arr]; // like arr.slice() arr2.push(4); // arr2 becomes [1, 2, 3, 4] // arr remains unaffectedSpread syntax effectively goes one level deep while copying an array. Therefore, it may be unsuitable for copying multidimensional arrays. The same is true with Object.assign() — no native operation in JavaScript does a deep clone. The web API method structuredClone() allows deep copying values of certain supported types. See shallow copy for more details.
jsconst a = [[1], [2], [3]]; const b = [...a]; b.shift().shift(); // 1 // Oh no! Now array 'a' is affected as well: console.log(a); // [[], [2], [3]]A better way to concatenate arrays
Array.prototype.concat() is often used to concatenate an array to the end of an existing array. Without spread syntax, this is done as:
jslet arr1 = [0, 1, 2]; const arr2 = [3, 4, 5]; // Append all items from arr2 onto arr1 arr1 = arr1.concat(arr2);With spread syntax this becomes:
jslet arr1 = [0, 1, 2]; const arr2 = [3, 4, 5]; arr1 = [...arr1, ...arr2]; // arr1 is now [0, 1, 2, 3, 4, 5]Array.prototype.unshift() is often used to insert an array of values at the start of an existing array. Without spread syntax, this is done as:
jsconst arr1 = [0, 1, 2]; const arr2 = [3, 4, 5]; // Prepend all items from arr2 onto arr1 Array.prototype.unshift.apply(arr1, arr2); console.log(arr1); // [3, 4, 5, 0, 1, 2]With spread syntax, this becomes:
jslet arr1 = [0, 1, 2]; const arr2 = [3, 4, 5]; arr1 = [...arr2, ...arr1]; console.log(arr1); // [3, 4, 5, 0, 1, 2]Note: Unlike unshift(), this creates a new arr1, instead of modifying the original arr1 array in-place.
Conditionally adding values to an array
You can make an element present or absent in an array literal, depending on a condition, using a conditional operator.
jsconst isSummer = false; const fruits = ["apple", "banana", ...(isSummer ? ["watermelon"] : [])]; // ['apple', 'banana']When the condition is false, we spread an empty array, so that nothing gets added to the final array. Note that this is different from the following:
jsconst fruits = ["apple", "banana", isSummer ? "watermelon" : undefined]; // ['apple', 'banana', undefined]In this case, an extra undefined element is added when isSummer is false, and this element will be visited by methods such as Array.prototype.map().
Từ khóa » Toán Tử Spread
-
ES6 Spread Operator - Viblo
-
Toán Tử (...) đã Thay đổi Javascript (P2) - Viblo
-
Toán Tử Spread được Sử Dụng để Làm Gì Trong JavaScript?
-
Toán Tử Spread JavaScript - Tech Wiki
-
Toán Tử Spread Trong JavaScript - TEK4
-
Toán Tử Spread JavaScript - Codefly.Vn
-
Toán Tử Spread Và Các Trường Hợp Sử Dụng Của Nó Trong JavaScript
-
Sử Dụng Spread Operator Trong ES6 - Le Vu Nguyen
-
Bài 8: ES6 - Toán Tử Spread Và Rest - YouTube
-
Toán Tử Spread JavaScript: Hướng Dẫn Hoàn Chỉnh
-
Sử Dụng Toán Tử Spread Trong Javascript - 2KVN
-
Sử Dụng Toán Tử Spread Trong Javascript - Trang Chủ - .vn
-
Spread Operator Và Rest Parameters Trong Javascript
-
Bài 8: ES6 - Toán Tử Spread Và Rest - TEDU
-
Bài 8: ES6 - Toán Tử Spread Và Rest 2023 - Mcongnghe.Com
-
JavaScript: Tìm Hiểu Về Hàm Hủy (destructuring), Tham Số Rest Và Cú ...
-
Nó Là “cú Pháp” Spread Hay “toán Tử” Spread? - HelpEx
-
Các Hàm Xử Lý Array - Mảng Trong Javascript - Code Từ Tâm
-
Một Số Kỹ Thuật Rút Gọn Code Javascript - Kaopiz Kipalog