Skip to content

Instantly share code, notes, and snippets.

@tgmarinho
Forked from sibelius/unique.tsx
Created June 22, 2021 14:52
Show Gist options
  • Select an option

  • Save tgmarinho/e38de621e49c89dd010730f352f47f14 to your computer and use it in GitHub Desktop.

Select an option

Save tgmarinho/e38de621e49c89dd010730f352f47f14 to your computer and use it in GitHub Desktop.
filter unique elements in an array
const simpleCompare = (obj, elem) => obj === elem;
export const unique = <T extends object>(data: T[], compareFn: (obj: T, elem: T) => boolean = simpleCompare): T[] => {
return data.filter((elem, pos, arr) => arr.findIndex(obj => compareFn(obj, elem)) === pos);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment