An Interest In:
Web News this Week
- March 28, 2024
- March 27, 2024
- March 26, 2024
- March 25, 2024
- March 24, 2024
- March 23, 2024
- March 22, 2024
June 13, 2021 02:32 pm GMT
Original Link: https://dev.to/abu/how-to-remove-duplicates-element-from-array-147l
How to remove duplicates element from array
In this post i'm going to remove duplicates element from array,i will try with multiple approaches to remove duplicates element from array.
Method 1
Using Set
const array = ["a","b","c","d","a","c","e","f"];const m1 = [...new Set(array)];console.log(m1);// ["a", "b", "c", "d", "e", "f"]
Method 2
Using object
const array = ["a","b","c","d","a","c","e","f"];let obj = {};for (let arr of array){obj[arr]=true;}console.log(Object.keys(obj));// ["a", "b", "c", "d", "e", "f"]
Method 3
Using Loop
const array = ["a","b","c","d","a","c","e","f"];const m3 = [];for(var i = 0;i<array.length;i++){const arr = array[i];m3.indexOf(arr) === -1 && m3.push(arr);}console.log(m3)// ["a", "b", "c", "d", "e", "f"]
Method 4
Using Filter
const array = ["a","b","c","d","a","c","e","f"];const m4 = array.filter((el,index)=>array.indexOf(el) == index);console.log(m4);// ["a", "b", "c", "d", "e", "f"]
Original Link: https://dev.to/abu/how-to-remove-duplicates-element-from-array-147l
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To