Home  >  Article  >  Web Front-end  >  What are the ways to use set to deduplicate arrays in javascript?

What are the ways to use set to deduplicate arrays in javascript?

青灯夜游
青灯夜游Original
2021-11-04 17:03:3516515browse

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)]".

What are the ways to use set to deduplicate arrays in javascript?

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:

What are the ways to use set to deduplicate arrays in javascript?

What are the ways to use set to deduplicate arrays in javascript?

## You can also use the Set object with the expansion operator

to deduplicate the array.

Grammar:


[...new Set(arr)]

Example:


What are the ways to use set to deduplicate arrays in javascript?

What are the ways to use set to deduplicate arrays in javascript?##【Recommended Learning :

javascript advanced tutorial

The 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!

Statement:
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