Skip to content

Instantly share code, notes, and snippets.

@lffg
Created December 8, 2021 16:09
Show Gist options
  • Select an option

  • Save lffg/b16fc77c97be4dbe9bc794e0f192ee21 to your computer and use it in GitHub Desktop.

Select an option

Save lffg/b16fc77c97be4dbe9bc794e0f192ee21 to your computer and use it in GitHub Desktop.
const data = [
{ id: 1, name: 'Foo-Dup' },
{ id: 1, name: 'Foo' },
{ id: 2, name: 'Bar' },
{ id: 3, name: 'Baz-Dup' },
{ id: 3, name: 'Baz' }
];
function dedupByKey(key, arr) {
const map = new Map();
for (const obj of arr) {
map.set(obj[key], obj);
}
return [...map.values()];
}
console.log(dedupByKey('id', data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment