dupes([{ name: 'x' }, { name: 'x' }, { name: 'y' }, { name: 'z' }], 'name')
Last active
April 23, 2021 11:45
-
-
Save srph/c62e63c11ea830544172eb9b8e7a85b0 to your computer and use it in GitHub Desktop.
JS: Get duplicate values in an array of 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 dupes = (arr, key) => { | |
const map = {} | |
const result = [] | |
arr.forEach((obj) => { | |
const value = obj[key] | |
if (map[value]) result.push(obj) | |
map[value] = true | |
}) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment