-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.helper.ts
More file actions
36 lines (28 loc) · 902 Bytes
/
Copy patharray.helper.ts
File metadata and controls
36 lines (28 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
export function pluck<T, K>(array: T[], key: string): K[] {
return array.map((a) => a[key]);
}
export const pick = (obj, arr) => arr.reduce((acc, record) => (record in obj && (acc[record] = obj[record]), acc), {});
export function shuffle<T>(array: T[]): T[] {
return array.sort(() => Math.random() - 0.5);
}
export function uniq<T>(array: T[]): T[] {
return Array.from(new Set(array));
}
export function reduceToObject<T>(array: T[], key: string): { [K: string]: T } {
return array.reduce((acc, curr) => {
acc[curr[key]] = curr;
return acc;
}, {});
}
export function groupBy<T>(array: T[], key: string): { [key: string]: T[] } {
return array.reduce(
(acc, curr) => {
if (!Object.prototype.hasOwnProperty.call(acc, curr[key])) {
acc[curr[key]] = [];
}
acc[curr[key]].push(curr);
return acc;
},
{} as { [key: string]: T[] }
);
}