I have this situation:
const array1 = [
{
node:
{
id: '789dg020hgn20ijahq',
family: 'ps4',
featuredImage: 'URL'
}
},
{
node:
{
id: 'gf80s70ga09ggds90ds000s9',
family: 'xbox-one',
featuredImage: 'URL'
}
}
]
const array2 = [
{
node:
{
id: 'h83g01whiehq8haos',
family: 'nintendo-switch',
gameName: 'Mario Cart'
}
},
{
node:
{
id: '1290qas9a0k19po1',
family: 'xbox-one',
gameName: 'COD WWII'
}
},
{
node:
{
id: '09ga09guh3njf32olkls',
family: 'xbox-one',
gameName: 'GOW 2'
}
}
]
What I am trying to achieve is to group these arrays by family key and have this new array:
*output I want*
[{
family: {
id: 'gf80s70ga09ggds90ds000s9',
family: 'xbox-one',
featuredImage: 'URL',
games: [
0: {
id: '1290qas9a0k19po1',
family: 'xbox-one',
gameName: 'COD WWII'
},
1: {
id: '09ga09guh3njf32olkls',
family: 'xbox-one',
gameName: 'GOW 2'
}
]
},
family: {
id: 'h83g01whiehq8haos',
family: 'nintendo-switch',
featuredImage: 'URL',
games: []
}
]
*if the family of the array1 is not matched in array2*
family: {
id: '789dg020hgn20ijahq',
family: 'ps4',
featuredImage: 'URL',
games: []
}
Using this code, I grouped the arrays by family, but I missed some informations. How can I modify the following code to have the desired result? Sorry but I am a little bit stuck....
const testing2 = result[1].map((obj) => { // result[1] comes from a Promise
return obj.node
})
_.mapValues(_.groupBy(testing2, 'family'), (list) => list.map((o) => _.omit(o, 'family')))
manufacturerwithfamily