Home > Article > Operation and Maintenance > How to implement array deduplication in Javascript
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' ]
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!