Remove duplicate objects/data from an array of objects in Javascript

How to Remove duplicate object data from an array using JavaScript.

To remove the duplicate object data from an array, have to use the filter function. So, it will filter out the duplicate objects/data from an array.

Please see the below example.

Javascript
var uniqueItems = [];

var sampleData =
  [
    { id: 1, name: 'firstname' },
    { id: 2, name: 'middlename' },
    { id: 1, name: 'firstname' },
    { id: 1, name: 'lastname' }
  ]

var duplicateItems = sampleData.filter(filteredData => {
  if (uniqueItems.find(items =>
    items.id === filteredData.id)) {
    return true;
  }
  uniqueItems.push(filteredData);
  return false;
});

Comments

Popular posts from this blog

Difference of High-Level Design (HLD) and Low-Level Design (LLD)

Angular CLI Commands - Cheat Sheet

Restrict Special Characters in Windows Application using c#

Naming Conventions

Configure the API gateway in Microservices Architecture with example in .NET Core

Restrict Special Characters Using JavaScript By Copy,Paste

Create a function to convert from string to Decimal using JavaScript

Recommended Visual Studio Code Extensions for Angular Development

Send API POST Request in MS SQL Server

Implementing DIP (Dependency Inversion Principle) in .NET Core project