Detailed explanation of js array deduplication example

韦小宝
Release: 2018-03-14 18:45:26
Original
1662 people have browsed it

There are many ways to deduplicate arrays in js, but today we will use the latest function of es6 to achieve a more concise solution. The two methods to be introduced now are related to es6’s new data structure Set. Let’s briefly introduce Set first. ES6 provides a new data structure Set. It is similar to an array, but the values of the members are unique and there are no duplicate values.

Set itself is a constructor used to generate Set data structure. Can accept an array as parameter for initialization.

Option one:

Use Set combined with expander

const set = new Set([1, 2, 3, 4, 4]);[...set]// ==> [1, 2, 3, 4]
Copy after login

Option two:

Use Set combined with Array.form, Because the data type returned by new Set() is not a data type, Array.form() is used to format and convert it into an ordinary array.

const set = Array.form(new Set([1, 2, 3, 4, 4]));// ==> [1, 2, 3, 4]
Copy after login

Related recommendations:

Six ways to share JS array deduplication

detailed explanation of js array deduplication and deflattening

Detailed examples of several ideas for deduplicating javascript arrays

The above is the detailed content of Detailed explanation of js array deduplication example. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!