In this post, We are going to learn how to Find Unique Values in an array of objects in JavaScript ES6 with an example. We will use Javascript Set that unique values collection, spread operator, and array.map() ,reduce() and simple for loop to get unique values.
1. Find Unique Values in array of object JavaScript ES6
A set object is a unique values collection of a different datatype which includes the primitive type or complex object literal, Array. It makes us iterate the elements in insertion order. In this JavaScript Example, We have used array.map() method will create a new array by selecting a desired property from the object array. The set constructor along with the spread operator selects unique property from a given array of objects.
let Employee = [ {Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'}, {Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'}, {Empname: 'Racx', salary: 60000,EmpID:4,Dep:'IT'},
{Empname: 'Racx', salary: 60000,EmpID:4,Dep:'IT'}
]
const uniqueVals = [...new Set(Employee.map(item => item.Empname))];
console.log(uniqueVals)
Output
[ 'John', 'jack', 'Racx' ]
2. Find all Unique values by key in Array of objects
In this example, we have found all unique values by key ‘Empname’ in all objects of the array by using the array.map() by selecting all the values. map constructor is used along with the spread operator to spread the array elements returned by the array.map() method.
let Employee = [
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
{Empname: 'Racx', salary: 60000,EmpID:4,Dep:'IT'},
{Empname: 'Racx', salary: 60000,EmpID:4,Dep:'IT'}
]
const key = 'Empname';
const uniqueVals = [...new Map(Employee.map(item =>
[item[key], item])).values()];
console.log(uniqueVals)
Output
[
{ Empname: 'John', salary: 60000, EmpID: 1, Dep: 'Admin' },
{ Empname: 'jack', salary: 50000, EmpID: 2, Dep: 'IT' },
{ Empname: 'Racx', salary: 60000, EmpID: 4, Dep: 'IT' }
]
3. Find all unique values in Array of objects
In this JavaScript program Example, We will discuss how to Find Unique Values in array of objects JavaScript by using array.filter() to get all uniques values by key ‘EmpId’ in all objects in the array.
let Employee = [
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
{Empname: 'Racx', salary: 60000,EmpID:4,Dep:'IT'},
{Empname: 'Racx', salary: 60000,EmpID:4,Dep:'IT'}
]
let uniqueVals = Employee.filter((value, index, self) => self.findIndex((Emp) => Emp.EmpID === value.EmpID) === index);
console.log(uniqueVals)
Output
[
{ Empname: 'John', salary: 60000, EmpID: 1, Dep: 'Admin' },
{ Empname: 'jack', salary: 50000, EmpID: 2, Dep: 'IT' },
{ Empname: 'Racx', salary: 60000, EmpID: 4, Dep: 'IT' }
]
4. Array.from() to Find unique values in Array of objects
In this example, We have used array.map() to get a new array that contains EmpID and set constructor to get uniques EmpID from an array of objects. The used array.from() instead of spread operator,array.from() is an static method that create an array object from array=like or iterable objects(string,array,map,set).
let Employee = [
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
{Empname: 'Racx', salary: 60000,EmpID:4,Dep:'IT'},
{Empname: 'Racx', salary: 60000,EmpID:4,Dep:'IT'}
]
let uniqueVals = Array.from(new Set(Employee.map((item => item.EmpID))))
console.log(uniqueVals)
Output
[ 1, 2, 4 ]
5. Reduce() to Find unique Values in array of object
To find unique values in all object in an array by EmpID, We have used array.map() that create a new array by EmpID from the Employee object. The array.reduce() method call a callback function for each element of the array and the result returned by the callback function after each iteration pass to the accumulator of reduce function that is used for the next iteration.
let Employee = [
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
{Empname: 'jack', salary: 50000,EmpID:2,Dep:'IT'},
{Empname: 'Racx', salary: 60000,EmpID:4,Dep:'IT'},
{Empname: 'Racx', salary: 60000,EmpID:4,Dep:'IT'}
]
let uniqueVals = Object.keys(Employee.reduce((result,{EmpID}) => (result[EmpID]='', result) , {}))
console.log(uniqueVals)
Output
[ '1', '2', '4' ]
6. Reduce() to Find unique value in Array of object
In this Javascript example to get the unique value of the property we have used reduce() function along with the spread operator that is used to spread the element.In the last step used a new set constructor to get unique values of EmpID.Let us understand with examples
let uniqueVals = [...new Set(Employee.reduce((res, Emp) => [...res, Emp.EmpID], []))];
console.log(uniqueVals)
Output
[ 1, 2, 4 ]
7. For loop to find unique value in Array of object
In this example, we have used simply for loop in javascript to find unique values in an array of objects.
var uniqueArr = [];
var result = [];
for( let i = 0; i < Employee.length; i++ ){
if( !uniqueArr[Employee[i].EmpID]){
result.push(Employee[i].EmpID);
uniqueArr[Employee[i].EmpID] = 1;
}
}
console.log(result)
Output
[ 1, 2, 4 ]
Summary
In this post, we have learned how to Find Unique Values in array of objects in JavaScript ES6.