Home  >  Article  >  Operation and Maintenance  >  How to implement array deduplication in Javascript

How to implement array deduplication in Javascript

王林
王林forward
2023-05-13 14:07:261458browse

Array deduplication

1. from() overlay new Set() method

The from method can be used directly to deduplicate string or numeric arrays.

var plants = ['Saturn', 'Earth', 'Uranus', 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter'];
var uniquePlants = Array.from(new Set(plants)); 
console.log(uniquePlants); // [ 'Saturn', 'Earth', 'Uranus', 'Mercury', 'Venus', 'Mars', 'Jupiter' ]

2. spread operator (...)

The spread operator is a major innovation of ES6 and has many powerful functions.

var plants = ['Saturn', 'Earth', 'Uranus', 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter'];
var uniquePlants = [...new Set(plants)]; 
console.log(uniquePlants); // [ 'Saturn', 'Earth', 'Uranus', 'Mercury', 'Venus', 'Mars', 'Jupiter' ]

The above is the detailed content of How to implement array deduplication in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete