1 回答
TA贡献1946条经验 获得超4个赞
我认为您的意思是您想要将输入中的每个对象拆分为单独的“已批准”和“已拒绝”对象。如果这是正确的,您可以从map()
const records = [{timeDate: '2020-12-10T06:00:00.535+00:00',totTransApproved: 75,totTransDeclined: 3,totAmount: 5016}, {timeDate: '2020-12-10T06:01:00.535+00:00',totTransApproved: 71,totTransDeclined: 4,totAmount: 11337}, {timeDate: '2020-12-10T06:02:00.535+00:00',totTransApproved: 83,totTransDeclined: 6,totAmount: 14370}]
const datums = records.map((item) => (
[
{ timeDate: item.timeDate,
name: 'Approved',
totTrans: item.totTransApproved,
totAmount: item.totAmount
},
{ timeDate: item.timeDate,
name: 'Declined',
totTrans: item.totTransDeclined,
totAmount: item.totAmount
}
]
));
console.log(datums);
.as-console-wrapper { max-height: 100% !important; top: 0; }
或者,如果您不希望它们按数组分组,请使用flatMap()展平返回的对象数组。
const datums = records.flatMap((item) => (
[
{ timeDate: item.timeDate,
name: 'Approved',
totTrans: item.totTransApproved,
totAmount: item.totAmount
},
{ timeDate: item.timeDate,
name: 'Declined',
totTrans: item.totTransDeclined,
totAmount: item.totAmount
}
]
));
添加回答
举报