
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Duplicate Records from Array in JavaScript
Let’s say the following are our records with some duplicate values −
var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ];
Example
To remove duplicate records, use the concept of Set().Following is the code −
var objectOfNationality = [ { nationality: "Indian" }, { nationality: "American" }, { nationality: "Emirati" }, { nationality: "Indian" }, { nationality: "American" } ]; function removeOrFilterNationality(objectOfNationality) { return Array.from(new Set(objectOfNationality.map(tempObject => tempObject.nationality))); } console.log(removeOrFilterNationality(objectOfNationality));
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo228.js.
Output
The output is as follows −
PS C:\Users\Amit\JavaScript-code> node demo228.js [ 'Indian', 'American', 'Emirati' ]
Advertisements