Created
January 9, 2020 18:00
-
-
Save krohne/452456713dd0827d6d1ae10cef8bd2c3 to your computer and use it in GitHub Desktop.
Eliminate duplicate array objects
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 arr = [ | |
{ id: 1, val: 'a' }, | |
{ id: 2, val: 'b' }, | |
{ id: 1, val: 'a' }, | |
{ id: 1, val: 'c' } | |
]; | |
const uni = arr | |
.reduce( ( unique, item ) => | |
unique.some( existing => | |
item.id === existing.id | |
&& item.val === existing.val) | |
? unique | |
: [...unique, item], | |
[] ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment