I'm trying to wrap my head around some more advanced uses of .reduce(), but still have a long way to go. The current issue has left me kind of clueless.
Let's say I have a an array of objects that looks like this:
const people = [
{
name: "Henry",
id: "1934",
wormsEaten: 4
},
{
name: "Melinda",
id: "9283",
wormsEaten: 0
},
{
name: "James",
id: "1029",
wormsEaten: 4
},
{
name: "Charles",
id: "7210",
wormsEaten: 3
},
{
name: "Sasha",
id: "4431",
wormsEaten: 3
},
]
I now want to create a new array of arrays for each matching wormsEaten value, i.e. an array that looks like this:
[
[
{
name: "Henry",
id: "1934",
wormsEaten: 4
},
{
name: "James",
id: "1029",
wormsEaten: 4
}
],
[
{
name: "Melinda",
id: "9283",
wormsEaten: 0
}
],
[
{
name: "Charles",
id: "7210",
wormsEaten: 3
},
{
name: "Sasha",
id: "4431",
wormsEaten: 3
}
]
]
My pseudocode-thinking brain says something along the lines of:
people.reduce((accumulator, currentPerson) => {
if(accumulator.wormsEaten === currentPerson.wormsEaten) {
return [
…
]
}
})
But that's roughly how far I get. I feel like I have a long way to go with this type of thinking and would be thankful if someone could help me in the right direction.