In es6, the spread operator "..." cannot be used alone to deduplicate the array. It can be used with the Set object to deduplicate the array. Deduplication method: 1. Use the "new Set(arr)" statement to convert the array to a Set collection type, and use the Set feature to remove duplicate elements; 2. Use the "[...set]" statement to convert the deduplicated Set collection. is an array.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
In es6, the spread operator "..." alone cannot be used to deduplicate the array. It can be used with the Set object to deduplicate the array.
Set is a new data structure provided by ES6, similar to an array, but itself has no duplicate values . Using this feature, we can convert the array to a Set type for deduplication, and then use the Array.from method to convert it to an array again.
The spread operator...
is introduced in ES6, which expands the iterable object into its separate elements. The so-called iterable object is anything that can be usedfor of
Objects that are traversed by loop, such as arrays, strings, Maps, Sets, DOM nodes, etc.
Implementation idea:
After converting the array into a set collection to remove duplication, use the expansion operator ...
Expand into an array and convert the collection into an array
Implementation code:
let arr=[1, 2, 3,3,2,"1",0,undefined,undefined]; let newArr=[...new Set(arr)]; console.log(newArr);
[Related recommendations:javascript video Tutorial、web front-end】
The above is the detailed content of Can es6 extension operator remove duplicates?. For more information, please follow other related articles on the PHP Chinese website!