How to use Map In Javascript
The Map object holds key-value pairs where keys may be any value (including objects or primitive values). By using the Map object, we can keep track of unique elements efficiently.
Example: In this example, we will utilize the Map object to remove duplicates from the array.
let arr = ["apple", "mango",
"apple", "orange", "mango", "mango"];
function removeDuplicates(arr) {
let uniqueMap = new Map();
arr.forEach(element => {
uniqueMap.set(element, null);
});
return Array.from(uniqueMap.keys());
}
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:
Contact Us