UNPKG

coffee-core

Version:

Coffee IT API core library

22 lines (19 loc) 652 B
export const shuffleArray = <T>(array: T[]): T[] => { return array .map((value) => ({ value, sort: Math.random() })) .sort((a, b) => a.sort - b.sort) .map((obj) => obj.value); }; export const removeListDuplicates = <T>(array: T[]): T[] => { return [...new Set(array)]; }; // source: https://stackoverflow.com/questions/15125920/how-to-get-distinct-values-from-an-array-of-objects-in-javascript export const removeListDuplicatesByKey = <T extends Record<string, any>>( array: T[], key: string, ): T[] => { if (array.length === 0) { return array; } return [...new Map(array.map((item) => [item[key], item])).values()]; };