1 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
我認(rèn)為您的意思是您想要將輸入中的每個(gè)對象拆分為單獨(dú)的“已批準(zhǔn)”和“已拒絕”對象。如果這是正確的,您可以從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; }
或者,如果您不希望它們按數(shù)組分組,請使用flatMap()展平返回的對象數(shù)組。
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
}
]
));
添加回答
舉報(bào)