Remove duplicates from array in javascript

Remove duplicates from array in javascript

JavaScript 24-07-2021 Saheb Sutradhar

In this post i will show you how to remove duplicates from array in javascript.

 

1. We can use filter() method to remove duplicates from array , if the index of the element gets matched with the indexOf value then only that element will get added.

let data = [1, 2, 3, 2, 1];

let uniqueData = data.filter((c, index) => {
    return data.indexOf(c) == index;
});

console.log(uniqueData);
Output - [ 1, 2, 3 ]

 

2. Using we can remove duplicate from array , Set is nothing but a collection of unique data , we used set to convert the array into Set to remove the duplicates, then using spread operator we have converted back the Set into array again.

let data = [1, 2, 3, 2, 1];
let setData= new Set(data);
let uniqueData = [... setData]
console.log(uniqueData);
Output - [ 1, 2, 3 ]

 

3. Another way to remove duplicate from array . using forEach and includes method. 

   array.includes(element) if the element if there in the array the include method will return true else false. so below example    we are iterating the data array using forEach loop and inside of it we are checking if() uniqueData array is having each the data element or not , if not then we are pushing that element to the uniqueData array.

 

let data = [1, 2, 3, 2, 1];
let uniqueData=[];

data.forEach(elem=>{
  if(!uniqueData.includes(elem)){
    uniqueData.push(elem);
  }
})
console.log(uniqueData);
Output - [ 1, 2, 3 ]

 

You can choose any of the three examples to remove duplicate from array in your project.

 

I hope this article is helpful for you , i am very glad to help you thanks for reading

 

 

 

Related Posts

Card image cap

Math Floor Javascript Method

JavaScript 16-07-2021 Saheb Sutradhar

math.floor javascript method takes a number type value as parameter and returns the round off of that value. The return value will be the nearest downward integ ...

Card image cap

Spread Operator in JavaScript

JavaScript 20-07-2021 Saheb Sutradhar

In this post we will learn what is spread operator in Javascript? How we can use it ? and Where we should use? Spread Operator allows you to spread out the ele ...

Card image cap

Next and Previous Buttons in Javascript with Demo

JavaScript 27-07-2021 Saheb Sutradhar

In this post we will learn how to use next and previous buttons in javascript with demo application , the below example there is  image container and next, pre ...

Card image cap

Image size validation in javascript example

JavaScript 28-07-2021 Saheb Sutradhar

In this article you will learn Image size validation in javascript with example , along with the size validation you will learn how to validate image mime type. ...

Card image cap

How to get multiple checkbox value in javascript

JavaScript 30-07-2021 Saheb Sutradhar

In this post you will learn how to get multiple checkbox value in javascript . In many application you will find this kind of feature where we allow user to sel ...

Card image cap

Multiple checkbox validation in javascript

JavaScript 02-08-2021 Saheb Sutradhar

In your application you can use single check box or you can use multiple checkbox . Below examples will help you to understand multiple checkbox validation in j ...