Skip to content

Instantly share code, notes, and snippets.

@srph
Last active April 23, 2021 11:45
Show Gist options
  • Save srph/c62e63c11ea830544172eb9b8e7a85b0 to your computer and use it in GitHub Desktop.
Save srph/c62e63c11ea830544172eb9b8e7a85b0 to your computer and use it in GitHub Desktop.
JS: Get duplicate values in an array of objects

usage

dupes([{ name: 'x' }, { name: 'x' }, { name: 'y' }, { name: 'z' }], 'name')
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