News for hackers. Stuff that matters.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by ReleaseCandidat on Wednesday January 05, 2022 @11:41AM (#29811249)
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, [])

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by christophilus on Wednesday January 05, 2022 @11:49AM (#29811370)
Push returns the length of the array, though, so that won't work.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by eyelidlessness on Wednesday January 05, 2022 @08:08PM (#29817720)
Comma 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!

Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by ReleaseCandidat on Wednesday January 05, 2022 @11:53AM (#29811428)
Ah, 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, [])
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by jasonkillian on Wednesday January 05, 2022 @12:15PM (#29811771)
Wouldn'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`.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by ReleaseCandidat on Wednesday January 05, 2022 @12:23PM (#29811897)
Yes, of course, that's why I used `push` at first.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by jacobolus on Wednesday January 05, 2022 @01:01PM (#29812544)
Use 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), [])
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by wk_end on Wednesday January 05, 2022 @01:15PM (#29812742)
If you're just iterating through the array and mutating an object on each iteration, just use a for loop.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by jacobolus on Wednesday January 05, 2022 @01:24PM (#29812871)
Obviously 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.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by wk_end on Wednesday January 05, 2022 @03:04PM (#29814197)
No 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);
  }
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by eyelidlessness on Wednesday January 05, 2022 @08:12PM (#29817747)
Minority 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.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by wk_end on Wednesday January 05, 2022 @11:19PM (#29819337)
Disagree: virtually everything in JS is mutable, so this almost means "never use the `const` keyword". Pretending that the `const` keyword means something that it doesn't makes things harder for my limited human mind to understand, not easier. Plus using `let` inappropriately makes my linter yells at me, and I usually like to just do whatever my linter tells me.

Anyway, I use TypeScript, so if I really want to assert that my array is immutable (as immutable as stuff in JS-land gets anyway) I just write:

  const input: readonly number[] = [1, 2, 3, 4, 5];
or even

  const input = [1, 2, 3, 4, 5] as const;
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by eyelidlessness on Wednesday January 05, 2022 @11:45PM (#29819506)
I realize I could be clearer in what I’m asking for: please use const when you use reference types as values, and use let when you intend to mutate the reference. Using const and then changing a value is certainly allowed but it’s confusing and it’s missing an opportunity to signal in the code where changes might happen.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by eyelidlessness on Wednesday January 05, 2022 @11:40PM (#29819470)
I `readonly` and `as const` everything I possibly can. I do know that const doesn’t mean immutable, as I said, but I think it should and I think there’s value in establishing the idiom even if it’s not currently adopted. Because otherwise const basically means nothing unless you’re mutating everything already.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by spiralx on Wednesday January 05, 2022 @12:46PM (#29812261)
Surely the spread operator is nicer here?

    [1, 2, 3, 4, 5].reduce((acc, n) => n % 2 === 1 ? [ ...acc, 2 * n ] : acc, [])
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by v413 on Wednesday January 05, 2022 @03:35PM (#29814558)
This is not efficient. Each iteration creates a new array instance due to the spread operator.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by spiralx on Tuesday January 11, 2022 @11:07AM (#29893209)
`acc.concat()` also creates a new array instance, so I don't get your point.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by eyelidlessness on Wednesday January 05, 2022 @08:03PM (#29817681)
Hilariously 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.
Re:Flatten Arrays in Vanilla JavaScript with Flat() and Flat... by pwdisswordfish9 on Wednesday January 05, 2022 @02:26PM (#29813664)
What is `f`reduce`?