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
}
]
));
添加回答
舉報