https://stackblitz.com/edit/angular-enctgg-dvagm3 Issue: Attempting to update the hours from arr2 to arr1 and create the desired output below.
Trying to achieve this using map function, but unsure how to navigate through nested arrays. Note: Array1 contains nested arrays which need to be mapped with array2 code :
var result = this.responseValue.map((person, index) => ({ ppmInfoId: { locationCode: person.locationCode, yearMonth: person.locationType, changeOrderId: 20 }, hours: index}));
Desired output :
"ppmInfo": [
{
"ppmInfoId": {
"changeOrderId": 0,
"locationCode": 1,
"yearMonth": "May/2021"
},
"hours": "10"
},
{
"ppmInfoId": {
"changeOrderId": 0,
"locationCode": 1,
"yearMonth": "June/2021"
},
"hours": "20"
},
{
"ppmInfoId": {
"changeOrderId": 0,
"locationCode": 2,
"yearMonth": "May/2021"
},
"hours": "10"
},
{
"ppmInfoId": {
"changeOrderId": 0,
"locationCode": 3,
"yearMonth": "May/2021"
},
"hours": "10"
}
]
Input Array 1 :
this.responseValue = [
{
locationCode: '1',
locationType: 'Onsite',
ppmDetailsToList: [
{
hours: 0,
changeOrderId: null,
yearMonth: 'May/2021',
},
{
hours: 0,
changeOrderId: null,
yearMonth: 'June/2021',
},
]
},
{
locationCode: '2',
locationType: 'Offshore',
ppmDetailsToList: [
{
hours: 0,
changeOrderId: null,
yearMonth: 'May/2021',
},
]
},
{
locationCode: '3',
locationType: 'offsite',
ppmDetailsToList: [
{
hours:0,
changeOrderId: null,
yearMonth: 'May/2021',
locationCode: 0
},
]
}
];
Input array 2 :
[{ "Onsite": [ 10, 20 ], "Offshore": [ 10 ], "offsite": [ 10 ] }]