2023-01-26 18:12:20 +00:00
|
|
|
export const groupBy = (array: any[], key: string) => {
|
|
|
|
const innerKey = key.split("."); // split the key by dot
|
|
|
|
return array.reduce((result, currentValue) => {
|
|
|
|
const key = innerKey.reduce((obj, i) => obj?.[i], currentValue) ?? "None"; // get the value of the inner key
|
|
|
|
(result[key] = result[key] || []).push(currentValue);
|
|
|
|
return result;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const orderArrayBy = (
|
|
|
|
array: any[],
|
|
|
|
key: string,
|
|
|
|
ordering: "ascending" | "descending" = "ascending"
|
|
|
|
) => {
|
2023-04-13 13:39:55 +00:00
|
|
|
if (key[0] === "-") {
|
|
|
|
ordering = "descending";
|
|
|
|
key = key.slice(1);
|
|
|
|
}
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
const innerKey = key.split("."); // split the key by dot
|
2023-04-13 13:39:55 +00:00
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
return array.sort((a, b) => {
|
|
|
|
const keyA = innerKey.reduce((obj, i) => obj[i], a); // get the value of the inner key
|
|
|
|
const keyB = innerKey.reduce((obj, i) => obj[i], b); // get the value of the inner key
|
|
|
|
if (keyA < keyB) {
|
|
|
|
return ordering === "ascending" ? -1 : 1;
|
|
|
|
}
|
|
|
|
if (keyA > keyB) {
|
|
|
|
return ordering === "ascending" ? 1 : -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
};
|