Spread Syntax (...) - JavaScript - MDN - 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 » Cách Dọc Spread
-
SPREAD | Phát âm Trong Tiếng Anh - Cambridge Dictionary
-
Cách Phát âm Spread Trong Tiếng Anh - Forvo
-
Spread - Wiktionary Tiếng Việt
-
Cấu Trúc Và Cách Dùng Spread Trong Tiếng Anh - StudyTiengAnh
-
Spread Nghĩa Là Gì ? | Từ Điển Anh Việt EzyDict
-
SPREAD - Nghĩa Trong Tiếng Tiếng Việt - Từ điển
-
Spread Tiếng Anh Là Gì? - Chickgolden
-
Spread Trong Tiếng Việt, Dịch, Tiếng Anh - Từ điển Tiếng Việt | Glosbe
-
Vietgle Tra Từ - Định Nghĩa Của Từ 'spread' Trong Từ điển Lạc Việt
-
Spread Position Nghĩa Là Gì Trong Tiếng Việt? - English Sticky
-
Câu Ví Dụ,định Nghĩa Và Cách Sử Dụng Của"Spread" - LIVESHAREWIKI
-
Spread Là Gì? Spread Trong Forex Có Gì Mà Thu Hút? - Coin28
-
Spread The Branch - (Bước 3 - Tỏa Cành) - Bản Tin Nóng