Last active
May 23, 2020 23:54
-
-
Save jefersonalmeida/4630702959b84899913b6e7f17aa1bf7 to your computer and use it in GitHub Desktop.
javascript - function to remove duplicate object via property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const removeDuplicates = (inputArray, prop) => | |
prop | |
? inputArray.filter((obj, pos, arr) => | |
obj[prop] ? arr.map((o) => o[prop]).indexOf(obj[prop]) === pos : true | |
) | |
: inputArray; | |
const array = [ | |
{ id: 1, name: 'Test 1' }, | |
{ id: 2, name: 'Test 2' }, | |
{ id: 3, name: 'Test 3' }, | |
{ id: 4, name: 'Test 1' }, | |
{ id: 4, name: 'Test 4' }, | |
]; | |
console.log(removeDuplicates(array, 'name')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment