How to use set() Method In Javascript
This method sets a new object type with ES6 (ES2015) that allows you to create collections of unique values.
Example: This example uses the JavaScript set() method to remove duplicates from an array.
let arr = ["apple", "mango", "apple",
"orange", "mango", "mango"];
function removeDuplicates(arr) {
return [...new Set(arr)];
}
console.log(removeDuplicates(arr));
Output
[ 'apple', 'mango', 'orange' ]
Remove Duplicate Elements from JavaScript Array
JavaScript arrays are the data structures that can store multiple values. However, there may be many cases where an array contains duplicate elements. This guide will walk you through various methods to remove these duplicates and create an array with unique elements.
Note: Learn More about JavaScript Arrays
Methods to Remove Duplicate Elements from JavaScript Array
Table of Content
- Using Javascript filter() Method
- Using Javascript set() Method
- Using Javascript forEach() Method
- Using Javascript reduce() Method
- Using Javascript indexOf() Method
- Using third-party Library