5 ms·
Yes, but you could also use `fold`^H^H^H^H`reduce`. [1, 2, 3, 4, 5].reduce((acc, n) => n % 2 === 1 ? acc.push(2*n) : acc, [])
by ReleaseCandidat 5y ago
Yes, but you could also use `fold`^H^H^H^H`reduce`.
[1, 2, 3, 4, 5].reduce((acc, n) => n % 2 === 1 ? acc.push(2*n) : acc, [])
- christophilus 5y agoPush returns the length of the array, though, so that won't work.
- ReleaseCandidat 5y agoAh, sorry, so you have to concat the arrays using `concat`. [1, 2, 3, 4, 5].reduce((acc, n) => n % 2 === 1 ? acc.concat([2*n]) : acc, [])
- spiralx 5y agoSurely the spread operator is nicer here? [1, 2, 3, 4, 5].reduce((acc, n) => n % 2 === 1 ? [ ...acc, 2 * n ] : acc, [])
- jasonkillian 5y agoWouldn't recommend doing this - if the original array is of significant length this'll get quite slow because `acc.concat` has to create a brand new array of slightly longer length on each iteration it's called. Better to just use `push` like you suggested before and then return the array if you want to use `reduce`.
- ReleaseCandidat 5y agoYes, of course, that's why I used `push` at first.
- jacobolus 5y agoUse the comma operator: (acc.push(2*n), acc) will return acc. Or e.g. [1, 2, 3, 4, 5].reduce((acc, n) => (n % 2 ? acc.push(2*n) : null, acc), [])
- wk_end 5y agoIf you're just iterating through the array and mutating an object on each iteration, just use a for loop.
- jacobolus 5y agoObviously you can alternately write: let input = [1, 2, 3, 4, 5], output = []; for (let i = 0; i < input.length; ++i) { let n = input[i]; if (n % 2) output.push(2*n); } return output; But in some circumstances the other style can be more convenient / legible. The immediate question was about pushing to an array and then returning the array, for which the comma operator can be handy.
- wk_end 5y agoNo argument that the comma operator is a neat trick when you need it. FWIW, it's 2022: const output = []; for (const n of [1, 2, 3, 4, 5]) { if (n % 2) output.push(2 * n); }
- eyelidlessness 5y agoMinority opinion: please `let` your mutable references. I know `const` doesn’t signal immutability, but we as humans with eyeballs and limited attention span certainly benefit from knowing when a value might change at runtime.
- eyelidlessness 5y agoComma operator to the rescue: (acc.push(2*n), acc) In an expression position, the push statement will be executed, then its return will be discarded, and the final expression will be the result of the expression. Is it “better”? Almost certainly not. But it lets you stay in expression syntax while executing statements. (Much more useful for logging than meaningful runtime side effects IMO, but I think it should be more widely known in general.) Edit: and I’m glad to see another reference to it down thread!
- pwdisswordfish9 5y agoWhat is `f`reduce`?
- eyelidlessness 5y agoHilariously enough, I think reduce would be much more palatable to JS devs if it was named fold. Not because the familiarity would jump out but because it’s a much more evocative name that could make visualizing its behavior more intuitive.