Created
October 10, 2025 12:20
-
-
Save orrisroot/d5ef2e077aa4982a072c7a9ff75475e5 to your computer and use it in GitHub Desktop.
arrayUnique - Removes duplicate items from an array based on a comparison function.
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
| /** | |
| * arrayUnique - Removes duplicate items from an array based on a comparison function. | |
| * | |
| * @param items The array to filter. | |
| * @param compare A function to compare two items for equality. | |
| * @returns A new array with duplicate items removed. | |
| * | |
| * Usage example: | |
| * const uniqueItems = arrayUnique<Item>(items, (a, b) => a.id === b.id); | |
| */ | |
| const arrayUnique = <T>(items: T[], compare: (a: T, b: T) => boolean = (a, b) => a === b): T[] => | |
| items.filter((item, index, self) => index === self.findIndex((v) => compare(v, item))); | |
| export { arrayUnique }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment