
- 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
Remove duplicates and map an array in JavaScript
Suppose, we have an array of objects like this −
const arr = [ {id:123, value:"value1", name:"Name1"}, {id:124, value:"value2", name:"Name1"}, {id:125, value:"value3", name:"Name2"}, {id:126, value:"value4", name:"Name2"} ];
Note that some of the "name" property in objects within the array are duplicate.
We are required to write a JavaScript function that takes in one such array of objects. The function should then construct a new array of strings that contains only unique "name" property value from the array.
Therefore, the output for the above input should look like this −
const output = ["Name1", "Name2"];
Example
The code for this will be −
const arr = [ {id:123, value:"value1", name:"Name1"}, {id:124, value:"value2", name:"Name1"}, {id:125, value:"value3", name:"Name2"}, {id:126, value:"value4", name:"Name2"} ]; const pickNames = (arr = []) =>{ const res = []; for (let i = arr.length; i−−;){ if (res.indexOf(arr[i].name) < 0) { res.push(arr[i].name); }; } return res; }; console.log(pickNames(arr));
Output
And the output in the console will be −
[ 'Name2', 'Name1' ]
- Related Articles
- Merge and remove duplicates in JavaScript Array
- Remove array duplicates by property - JavaScript
- Remove duplicates from an array keeping its length same in JavaScript
- Remove duplicates from array with URL values in JavaScript
- Remove duplicates from a array of objects JavaScript
- The best way to remove duplicates from an array of objects in JavaScript?
- How do I make an array with unique elements (remove duplicates) - JavaScript?
- Golang Program To Remove Duplicates From An Array
- Java Program to Remove Duplicates from an Array List
- Unique sort (removing duplicates and sorting an array) in JavaScript
- Remove Duplicates from Sorted Array in Python
- Remove Duplicates from Sorted Array II in C++
- Removing consecutive duplicates from strings in an array using JavaScript
- How to find duplicates in an array using set() and filter() methods in JavaScript?
- Counting duplicates and aggregating array of objects in JavaScript

Advertisements