Get all unique values in a JavaScript array
Arrays are a fundamental data structure in JavaScript and are used to store multiple values in a single variable. In some cases, we may need to remove duplicate values from an array and retrieve only the unique values. In this article, we will explore different approaches to solving this problem in JavaScript.
Method 1: Using the Set Object
The Set object is a built-in JavaScript object introduced in ES6 that allows you to store unique values of any type, whether they are primitive values or object references.
Here's an example of how to use the Set object to get unique values from an array:
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]
In the above example, we create a Set object by spreading the elements of the array using the spread syntax (...). This ensures that each element of the array is treated as a separate argument to the Set constructor. The Set object automatically removes any duplicate values. Finally, we convert the Set object back to an array using the spread syntax and store it in the uniqueArray variable.
It's important to note that the Set object preserves the order of the elements in the original array, so the resulting uniqueArray will have the same order.
Method 2: Using a For Loop and an Object
If you are working with JavaScript versions that do not support the Set object (e.g., older browsers), you can use a for loop and an object to achieve the same result:
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = [];
const obj = {};
for (let i = 0; i < array.length; i++) {
obj[array[i]] = true;
}
for (let key in obj) {
uniqueArray.push(parseInt(key));
}
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]
In the above example, we iterate over each element in the array using a for loop. We use an object (obj) to keep track of the unique values by assigning them as keys with a value of true. Finally, we convert the keys of the object back to integers and push them into the uniqueArray.
Method 3: Using the filter() Method
The filter() method is a built-in JavaScript method that allows you to create a new array with all elements that pass a certain test.
Here's an example of how to use the filter() method to remove duplicate values:
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = array.filter((value, index, self) => {
return self.indexOf(value) === index;
});
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]
In the above example, we use the filter() method on the array and pass a callback function as an argument. The callback function takes three parameters: the current value, the index of the current value, and the array itself (self). Inside the callback function, we check if the index of the current value in the array is the same as the current index. If they are equal, it means that the value is unique and should be included in the resulting array.
Method 4: Using the reduce() Method
The reduce() method is a powerful built-in JavaScript method that allows you to apply a function against an accumulator and each element in the array to reduce it to a single value.
Here's an example of how to use the reduce() method to remove duplicate values:
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = array.reduce((accumulator, currentValue) => {
if (!accumulator.includes(currentValue)) {
accumulator.push(currentValue);
}
return accumulator;
}, []);
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]
In the above example, we use the reduce() method on the array and pass a callback function and an initial value (an empty array) as arguments. Inside the callback function, we check if the accumulator (the array that accumulates the unique values) does not already include the current value. If the current value is not included, we push it into the accumulator. Finally, we return the updated accumulator.
Method 5: Using the Map Object
The Map object is another built-in JavaScript object introduced in ES6 that allows you to store key-value pairs where both the key and value can be of any type.
Here's an example of how to use the Map object to get unique values from an array:
const array = [1, 2, 3, 3, 4, 5, 5];
const uniqueArray = Array.from(new Map(array.map(value => [value, value])).values());
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]
In the above example, we use the map() method on the array to create a new array of key-value pairs. Each pair consists of the value itself as both the key and value. We then create a new Map object from this array of key-value pairs and convert it back to an array using the Array.from() method. Finally, we retrieve the values from the Map object and store them in the uniqueArray.
Conclusion
There are multiple ways to get unique values from a JavaScript array. The choice of method depends on the JavaScript version you are using and your preference in terms of syntax and performance. In this article, we explored five different methods: using the Set object, using a for loop and an object, using the filter() method, using the reduce() method, and using the Map object.
A good understanding of these methods will empower you to efficiently manage and manipulate arrays in your JavaScript applications.