How to use forEach() Method In Javascript
By using the forEach() method, we can iterate over the elements in the array, and we will push into the new array if it doesn’t exist in the array.
Example: In this example, we will see the use of the forEach() method.
let arr = ["apple", "mango",
"apple", "orange", "mango", "mango"];
function removeDuplicates(arr) {
let unique = [];
arr.forEach(element => {
if (!unique.includes(element)) {
unique.push(element);
}
});
return unique;
}
console.log(removeDuplicates(arr));
Output:
["apple", "mango", "orange"]
How to Remove duplicate elements from array in JavaScript ?
In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array.
These are the following ways:
Table of Content
- Using filter() Method
- Using set() Method
- Using forEach() Method
- Using reduce() Method
- Using indexOf() Method
- Using a third-party library
- Using Map
Below all the methods are described with proper examples: