Home > Web Front-end > Front-end Q&A > Can es6 extension operator remove duplicates?

Can es6 extension operator remove duplicates?

青灯夜游
Release: 2022-04-19 20:04:22
Original
2445 people have browsed it

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.

Can es6 extension operator remove duplicates?

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 ofObjects 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);
Copy after login

Can es6 extension operator remove duplicates?

[Related recommendations:javascript video Tutorialweb 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!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template