Skip to content

Instantly share code, notes, and snippets.

@employee451
Created May 30, 2022 11:57
Show Gist options
  • Save employee451/072d62e00dba6cb1a43680acd8007351 to your computer and use it in GitHub Desktop.
Save employee451/072d62e00dba6cb1a43680acd8007351 to your computer and use it in GitHub Desktop.
// @see https://gist.github.com/employee451/b4414c0cc4f5a818eea82f881c4dc227
import getNestedProperty from './getNestedProperty'
/**
* Returns an array without duplicate items based on the provided object property
*/
function removeArrayDuplicatesByProperty<ItemType = any>(
array: ItemType[],
property: string
): ItemType[] {
const seen = new Set()
return array.filter((item) => {
const propertyValue = getNestedProperty(item, property)
const isDuplicate = seen.has(propertyValue)
seen.add(propertyValue)
return !isDuplicate
})
}
export default removeArrayDuplicatesByProperty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment