How To Get Unique Object List From Array
In this article, we will learn how to get unique object list from array.
So, let's start with the implementation
Suppose you have an object list named players like shown below.
const Players = [ { id: 1, name: 'Kohli'}, { id: 2, name: 'Rohit'}, { id: 3, name: 'Dhoni'}, { id: 1, name: 'Kohli'}, { id: 2, name: 'Rohit'}, { id: 1, name: 'Kohli'}, { id: 2, name: 'Rohit'} ];
Additionally, you want to extract the distinct values of each item's id property from the array.
You can obtain the unique list by key as follows:
const key = 'id'; // set key to get unique list const unique = [...new Map(Players.map(item => [item[key], item])).values()]; console.log(unique);
By filtering the key in this case, you get a list of unique objects showing in below image.
Conclusion
In this article, we learned how to how to get unique object list from array.
I hope this article helps you and you will like it.👍
Please give your valuable feedback and comments or suggestion in the comment section
If you have any doubt or confusion then free to ask in the comment section.