
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
The best way to remove duplicates from an array of objects in JavaScript?
Let’s say the following is our array of objects with duplicates −
var studentDetails=[ {studentId:101}, {studentId:104}, {studentId:106}, {studentId:104}, {studentId:110}, {studentId:106}, ]
Use the concept of set to remove duplicates as in the below code −
Example
var studentDetails=[ {studentId:101}, {studentId:104}, {studentId:106}, {studentId:104}, {studentId:110}, {studentId:106}, ] const distinctValues = new Set const withoutDuplicate = [] for (const tempObj of studentDetails) { if (!distinctValues.has(tempObj.studentId)) { distinctValues.add(tempObj.studentId) withoutDuplicate.push(tempObj) } } console.log(withoutDuplicate);
To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo158.js. This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo158.js [ { studentId: 101 }, { studentId: 104 }, { studentId: 106 }, { studentId: 110 } ]
- Related Articles
- Remove duplicates from a array of objects JavaScript
- Remove duplicates from an array keeping its length same in JavaScript
- Golang Program To Remove Duplicates From An Array
- Remove duplicates and map an array in JavaScript
- Remove duplicates from array with URL values in JavaScript
- Java Program to Remove Duplicates from an Array List
- What is the best way to remove an item from a Python dictionary?
- Counting duplicates and aggregating array of objects in JavaScript
- Merge and remove duplicates in JavaScript Array
- Remove array duplicates by property - JavaScript
- Remove Duplicates from Sorted Array in Python
- Best way to flatten an object with array properties into one array JavaScript
- Search from an array of objects via array of string to get array of objects in JavaScript
- Remove Duplicates from Sorted Array II in C++
- What is the best way to reduce and merge a collection of objects – JavaScript?

Advertisements