DEV Community

Discussion on: 7 Ways to Iterate Over Arrays and When to Use Each

Collapse
 
icecoffee profile image
Atulit Anand • Edited

let allEntrees = orders.reduce(
(accumulator, currentValue) => [...accumulator, ...currentValue.entree]
)

In reduce() someone please explain this.

Uncaught TypeError: accumulator is not iterable
    at <anonymous>:2:38
    at Array.reduce (<anonymous>)
    at <anonymous>:1:25
(anonymous) @ VM134:2
(anonymous) @ VM134:1
Enter fullscreen mode Exit fullscreen mode

I get it what she's trying to do but still, console gave me an error.

Thanks for the article.
I didn't know about for...in and for...of before.

Collapse
 
christiankozalla profile image
Christian Kozalla • Edited

Well, I did not run the code above, but I'd suggest reading the documentation of reduce as a supplement

developer.mozilla.org/en-US/docs/W...

Anyways, some great examples 👍

Collapse
 
icecoffee profile image
Atulit Anand

Ty 😸 I did the math, why their is an error.

Collapse
 
lexlohr profile image
Alex Lohr
let allEntrees = orders.reduce(
(accumulator, currentValue) => 
  [...accumulator, ...currentValue.entree],
  [] // you forgot this here
)
Enter fullscreen mode Exit fullscreen mode

FTFY

Collapse
 
christinecontreras profile image
Christine Contreras

Thanks so much for catching this! updated.

Thread Thread
 
icecoffee profile image
Atulit Anand

Collapse
 
icecoffee profile image
Atulit Anand • Edited

lol never thought about this one, I used Object.values() instead and a tiny bit of mod in the logic.