Skip to content

Instantly share code, notes, and snippets.

@jefersonalmeida
Last active May 23, 2020 23:54
Show Gist options
  • Save jefersonalmeida/4630702959b84899913b6e7f17aa1bf7 to your computer and use it in GitHub Desktop.
Save jefersonalmeida/4630702959b84899913b6e7f17aa1bf7 to your computer and use it in GitHub Desktop.
javascript - function to remove duplicate object via property
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