I try to merge two arrays of objects and at the same time add their name as a property to the objects:
const firstGroup = [
{
firstname: 'Nick',
id: 1
},
{
firstname: 'Joe',
id: 2
},
]
const secondGroup = [
{
firstname: 'Tom',
id: 1
},
]
The desired output:
allGroups = [
{
groupname: 'firstGroup',
firstname: 'Nick',
id: 1
},
{
groupname: 'firstGroup',
firstname: 'Joe',
id: 2
},
{
groupname: 'secondGroup',
firstname: 'Tom',
id: 1
}
]
Here is what I tried:
Object
.entries({ firstGroup, secondGroup })
.reduce((acc, [name, [firstname, id]]) => ([{
...acc,
groupname: name,
firstname,
id
}]), [])
But it doesn't work and I can't figure out how I can do it.
Thanks for your help !