Home >Web Front-end >JS Tutorial >What are the ways to use set to deduplicate arrays in javascript?
Methods to deduplicate arrays: 1. Use the set object to cooperate with the "Array.from()" method of the array, the syntax is "Array.from(new Set(arr))"; 2. Use the Set object to cooperate with expansion Operator "...", syntax "[...new Set(arr)]".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript uses set to deduplicate arrays
In javascript, you can use the set object to cooperate with the array's "Array.from() ” method to perform array deduplication.
Set is a new data structure provided by ES6, which is similar to an array, but 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 Array.from method is used to convert two types of objects into real arrays: array-like objects and iterable objects (including ES6 new Additional data structures Set and Map).
Grammar:
Array.from(new Set(arr))
Example:
… to deduplicate the array.
[...new Set(arr)]Example:
##【Recommended Learning :
javascript advanced tutorialThe above is the detailed content of What are the ways to use set to deduplicate arrays in javascript?. For more information, please follow other related articles on the PHP Chinese website!