arr = [
{
day: "monday",
ages: [1,5,2]
},
{
day: "tuesday",
ages: [9]
},
{
day: "monday",
ages: [22,24,28]
},
{
day: "tuesday",
ages: [19]
},
]
I want the result to be an object that has each day as a key and the values as arrays:
expected:
{
monday: [1,5,2,22,24,28],
tuesday: [9,19]
}
I tried using reduce as follows:
x = arr.reduce((acc,curr) => {
acc[curr.day] = []
acc[curr.day].push(curr.ages)
return acc
})
but it gives me:
{
ages: [1,5,2],
day: "tuesday",
monday: [22,24,28],
tuesday: [19]
}
why would it add ages and day as keys?