Added keyBy and groupBy helpers

This commit is contained in:
Mitchell McCaffrey 2020-10-01 15:05:06 +10:00
parent 1938f59060
commit a67ff4f407

View File

@ -54,3 +54,18 @@ export function logImage(url, width, height) {
export function isEmpty(obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
export function keyBy(array, key) {
return array.reduce(
(prev, current) => ({ ...prev, [key ? current[key] : current]: current }),
{}
);
}
export function groupBy(array, key) {
return array.reduce((prev, current) => {
const k = current[key];
(prev[k] || (prev[k] = [])).push(current);
return prev;
}, {});
}