3 ms·
A better option (for me, who likes fp) would be writing a group_by reducer once and apply it to any situation at hand group_by = f => (acc, item) => ({...a
by ulucs 7y ago
A better option (for me, who likes fp) would be writing a group_by reducer once and apply it to any situation at hand
group_by = f => (acc, item) => ({...acc, [f(item)]: [...(acc[f(item)] || []), item]});
const {true: truthy, false: falsy} = values.reduce(group_by(Boolean), {});
Though as long as you don't need to keep writing for loops for each grouping you do, group_by using reduce or for loops doesn't really matter.