https://stackblitz.com/edit/angular-enctgg-dvagm3 Problem statement : Am trying to patch the hours from arr2 to arr1 and then build the whole expected o/p as given below.
I was trying with map. But not sure how to traverse through nested array. Note : Array1 has nested array and need to map those into arrar2 code :
var result = this.responseValue.map((person, index) => ({ ppmInfoId: { locationCode: person.locationCode, yearMonth: person.locationType, changeOrderId: 20 }, hours: index}));
Expected 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 ] }]