
- 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 from array with URL values in JavaScript
Suppose, we have an array of objects like this −
const arr = [ { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello-how-are-you', id: "23" }, { url: 'www.example.com/i-like-cats', id: "24" }, { url: 'www.example.com/i-like-pie', id: "25" } ];
We are required to write a JavaScript function that takes in one such array of objects. The function should remove such objects from the array that have duplicate id keys. We are required to do this without using any libraries like, underscore.
Let us write the code for this function −
Example
The code for this will be −
const arr = [ { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello−how−are−you', id: "23" }, { url: 'www.example.com/i−like−cats', id: "24" }, { url: 'www.example.com/i−like−pie', id: "25" } ]; const removeDuplicate = (arr = []) => { const map = {}; for(let i = 0; i < arr.length; ){ const { id } = arr[i]; if(map.hasOwnProperty(id)){ arr.splice(i, 1); }else{ map[id] = true; i++; }; }; }; removeDuplicate(arr); console.log(arr);
Output
And the output in the console will be −
[ { url: 'www.example.com/hello', id: '22' }, { url: 'www.example.com/hello-how-are-you', id: '23' }, { url: 'www.example.com/i-like-cats', id: '24' }, { url: 'www.example.com/i-like-pie', id: '25' } ]
- Related Articles
- Remove duplicates from a array of objects JavaScript
- 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 Sorted Array in Python
- Remove duplicates and map an array in JavaScript
- Remove Duplicates from Sorted Array II in C++
- Remove same values from array containing multiple values JavaScript
- The best way to remove duplicates from an array of objects in JavaScript?
- Golang Program To Remove Duplicates From An Array
- How do I make an array with unique elements (remove duplicates) - JavaScript?
- How to remove falsy values from an array in JavaScript?
- Java Program to Remove Duplicates from an Array List
- How to remove the hash from window.location (URL) with JavaScript without page refresh?
- Remove duplicates from a List in C#

Advertisements