Created
May 30, 2022 11:57
-
-
Save employee451/072d62e00dba6cb1a43680acd8007351 to your computer and use it in GitHub Desktop.
This file contains 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
// @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