[英]Create an object of arrays with combining two arrays
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. https://stackblitz.com/edit/angular-enctgg-dvagm3问题陈述:我试图修补从 arr2 到 arr1 的时间,然后构建整个预期的 o/p,如下所示。
I was trying with map.我正在尝试使用 map。 But not sure how to traverse through nested array.
但不确定如何遍历嵌套数组。 Note: Array1 has nested array and need to map those into arrar2 code:
注意:Array1 有嵌套数组,需要将 map 那些放入 arrar2代码:
var result = this.responseValue.map((person, index) => ({ ppmInfoId: { locationCode: person.locationCode, yearMonth: person.locationType, changeOrderId: 20 }, hours: index}));
Expected output:预期 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:输入数组 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:输入数组 2:
[{ "Onsite": [ 10, 20 ], "Offshore": [ 10 ], "offsite": [ 10 ] }]
const array1 = [{ locationCode: '1', locationType: 'Onsite', ppmDetailsToList: [{ hours: 0, changeOrderId: null, yearMonth: 'May/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 }, ] } ]; const array2 = [{ "Onsite": [10], "Offshore": [10], "offsite": [10] }] const result = { ppmInfo: array1.map(({ locationCode, locationType, ppmDetailsToList: [{ hours, changeOrderId, yearMonth }] }) =>({ hours: array2[0][locationType][0], ppmInfoId: { changeOrderId: changeOrderId || 0, locationCode, yearMonth } })) } console.log(result)
Used foreach itself to get the desired output.使用 foreach 本身来获得所需的 output。 Closing this
关闭这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.