4 ms·
You really should just use a for loop over reduce. It’s much easier to understand, shorter, and is more performant: const truthy = [], falsy = []; for
by tylerhou 7y ago
You really should just use a for loop over reduce. It’s much easier to understand, shorter, and is more performant:
const truthy = [], falsy = [];
for (const i of values) {
if (i) truthy.push(i);
else falsy.push(i);
}
- ulucs 7y agoA 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.
- paulddraper 7y agoconst truthy = [], falsy = []; for (const i of values) { (i ? truthy : falsy).push(i); } If for some reason you did want to use reduce (and I see no reason to), it would be const [truthy, falsey] = exampleValues.reduce((arrays, i) => { arrays[+!i].push(i); return arrays; }, [[], []]);
- deleted 7y ago[deleted]
- ElFitz 7y agoThat really is more a matter of taste than facts