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.
Please ask me if you need more information if it’s still not enough to solve that case.