- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 the duplicate value from array with images data in JavaScript
Suppose, we have some data regarding some images in an array like this −
const arr = [{ 'image': "jv2bcutaxrms4i_img.png", 'gallery_image': true }, { 'image': "abs.png", 'gallery_image': true }, { 'image': "acd.png", 'gallery_image': false }, { 'image': "jv2bcutaxrms4i_img.png", 'gallery_image': true }, { 'image': "abs.png", 'gallery_image': true }, { 'image': "acd.png", 'gallery_image': false }];
We are required to write a JavaScript function that takes in one such array.
Our function should remove the objects from the array that have duplicate values for the 'image' property.
Example
The code for this will be −
const arr = [{ 'image': "jv2bcutaxrms4i_img.png", 'gallery_image': true }, { 'image': "abs.png", 'gallery_image': true }, { 'image': "acd.png", 'gallery_image': false }, { 'image': "jv2bcutaxrms4i_img.png", 'gallery_image': true }, { 'image': "abs.png", 'gallery_image': true }, { 'image': "acd.png", 'gallery_image': false }]; const buildUnique = (arr = []) => { const unique = []; arr.forEach(obj => { let found = false; unique.forEach(uniqueObj => { if(uniqueObj.image === obj.image) { found = true; }; }); if(!found){ unique.push(obj); }; }); return unique; }; console.log(buildUnique(arr));
Output
And the output in the console will be −
[ { image: 'jv2bcutaxrms4i_img.png', gallery_image: true }, { image: 'abs.png', gallery_image: true }, { image: 'acd.png', gallery_image: false } ]
- Related Articles
- Remove/ filter duplicate records from array - JavaScript?
- Remove duplicate items from an array with a custom function in JavaScript
- How to remove duplicate elements from an array in JavaScript?
- Using recursion to remove consecutive duplicate entries from an array in JavaScript
- Using recursion to remove consecutive duplicate entries from an array - JavaScript
- Sum all duplicate value in array - JavaScript
- JavaScript Remove non-duplicate characters from string
- How to remove duplicate property values in array – JavaScript?
- Removing duplicate objects from array in JavaScript
- Remove duplicates from array with URL values in JavaScript
- Swift Program to Remove Duplicate Elements From an Array
- Golang Program To Remove Duplicate Elements From An Array
- How to remove an item from JavaScript array by value?
- How to redundantly remove duplicate elements within an array – JavaScript?
- Return the first duplicate number from an array in JavaScript

Advertisements