Created
December 8, 2021 16:09
-
-
Save lffg/b16fc77c97be4dbe9bc794e0f192ee21 to your computer and use it in GitHub Desktop.
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 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