[ { "vehicleId": "34b8413a541612cfdb53a35e00f2b17c", "vehicleCode": "京AF03128", "brandId": "福田", "vehicleVin": "LVBV3J0B0NW001886", "engineCode": null, "drivingSignDate": "2022-06-14 00:00:00", "createTime": "2022-11-16 23:44:58", "yxnEquipmentCode": null, "yxnSn": null, "signType": "046001", "signFlag": 1, "billPrice": 0, "signMileage": 0, "mileageCurrent": 0, "mileageLast": 0, "signStartTime": "2023-03-01 00:00:00" } ],修改上述数据结构,添加一个total属性,如果mileageLast=0,total的值为signMileage乘以billPrice,如果mileageLast不是0,total的值为mileageCurrent减去mileageLast乘以billPrice 前端 es6
时间: 2023-07-21 17:11:13 浏览: 100
你可以使用ES6的箭头函数和条件运算符来实现这个需求。以下是修改后的数据结构的示例代码:
```javascript
const data = [
{
"vehicleId": "34b8413a541612cfdb53a35e00f2b17c",
"vehicleCode": "京AF03128",
"brandId": "福田",
"vehicleVin": "LVBV3J0B0NW001886",
"engineCode": null,
"drivingSignDate": "2022-06-14 00:00:00",
"createTime": "2022-11-16 23:44:58",
"yxnEquipmentCode": null,
"yxnSn": null,
"signType": "046001",
"signFlag": 1,
"billPrice": 0,
"signMileage": 0,
"mileageCurrent": 0,
"mileageLast": 0,
"signStartTime": "2023-03-01 00:00:00"
}
];
const modifiedData = data.map(item => ({
...item,
total: item.mileageLast === 0 ? item.signMileage * item.billPrice : (item.mileageCurrent - item.mileageLast) * item.billPrice
}));
console.log(modifiedData);
```
在上述代码中,使用`map`方法遍历原始数据数组,并使用对象扩展运算符`...`将原始对象的所有属性复制到新对象中。然后,根据`mileageLast`的值使用条件运算符来计算`total`属性的值,并将新对象添加到`modifiedData`数组中。
请注意,这只是一个示例代码,你需要根据你的实际情况进行相应的调整。
阅读全文