I have an array of objects I need to transform into new objects. some value as keys and an array of values.
const data = [
{
type: 'user',
id: 1
},
{
type: 'user',
id: 2
},
{
type: 'group',
id: 1
},
{
type: 'group',
id: 2
},
{
type: 'user',
id: 3
},
{
type: 'user',
id: 4
}
]
and the desired result is
const result = {
user: [1, 2, 3, 4],
group: [1, 2]
}
what I've tried using reduced
but it not what I'm expected.
const result = data.reduce((acc, { type, ...obj }) => {
acc[type] = data.map(item => item.id)
return acc;
}, {})
result = { user: [ 1, 2, 1, 2, 3, 4 ], group: [ 1, 2, 1, 2, 3, 4 ] }