JavaScript

How To Get Unique Object From Array Using JavaScript

In this article, we will learn how to get a unique object from an array.

Suppose you have an object named players like shown below in an image:

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'}
  ];

And you want to get unique values of the id attribute of each item from the array.

Here’s how you can get the unique list by key:

const key = 'id'; // set key to get unique list

const unique = [...new Map(Players.map(item =>
  [item[key], item])).values()];

console.log(unique);

Here you get a unique object list by filtering the key.

Hope this article helps you. Thanks for reading.

Jignesh Patel

Jignesh Patel is a Senior Full Stack .Net Developer has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET MVC, AngularJS, Angular, Nodejs, Web API, EPPlus, SQL, Entity Framework, JavaScript, Azure Web Jobs, Microsoft Graph API, etc.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago