coffee-core
Version:
Coffee IT API core library
18 lines (15 loc) • 666 B
text/typescript
export const shuffleArray = <T extends unknown>(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 extends unknown>(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()];
};