|
|
|
|
|
by femto113
1631 days ago
|
|
The spread operator looks cool and makes just returning the ternary operator work here but its performance implications are non-obvious (it's makin' copies). With reduce() you're really wanting something like this: [1, 2, 3, 4, 5].reduce((x, y) => { if (y % 2 === 1) x.push(y * 2); return x; }, [])
I've many times wished that push() would just return the array, it would make reduce() far easier for this sort of use case. |
|
Anyway, I find this to be a whole lot more sensible:
Or even! I cant even tell what language this is but there is nothing here that needs fixing.