I have 2 array that consist object :
a =
[
{
id: 105647,
name: 'name1'
},
{
id: 105941,
name: 'name2'
},
{
id: 106177,
name: 'name3'
}
]
b =
[
[
{
id: 105647,
transactionDate: '2022-03-21',
order: 1
},
{
id: 105647,
transactionDate: '2022-03-22',
order: 2
}
],
[
{
id: 106177,
transactionDate: '2022-03-24',
order: 1
},
]
]
As you can see, that in b is not always have data for each id in variable a.
And my desired output would be
a =
[
{
id: 105647,
name: 'name1',
dataB:
[
{
id: 105647,
transactionDate: '2022-03-21',
order: 1
},
{
id: 105647,
transactionDate: '2022-03-22',
order: 2
}
]
},
{
id: 105941,
name: 'name2'
},
{
id: 106177,
name: 'name3',
dataB:
[
{
id: 106177,
transactionDate: '2022-03-24',
order: 1
},
]
}
]
the b variable should be pushed to new variable dataB inside object of array on variable a. And if no dataB exist on a it would not create dataB on a / create the dataB but with empty array.
I was already try with some
for (let i = 0; i<a.length; i++) {
a[i]['dataB'] = b[i]
}
the result is not pushed by id, but by index. The problem is b not always have data for each id in variable a. And what i'm trying was not what i looking for.
Please ask me if you need more information if it's still not enough to solve that case.
valuescorresponding to the ID keys (fromb) are arrays, then when you.mapovera, things become much more easier to transform elements ofaas per your target structure.