[ { "date": "2023-05-22", "orderList": [{ "id": 22, "schoolId": null, "schoolName": null, "name": null, "menuType": 1, "amount": 0.66, "refundAmount": null, "orderNo": null, "status": 3, "week": null, "orderDate": null, "detailList": [{ "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "大米炒鸡蛋", "price": null, "amount": 0.66, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "大米炒鸡蛋", "price": null, "amount": 0.66, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "红烧肉", "price": null, "amount": 0.01, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "大米炒鸡蛋", "price": null, "amount": 0.66, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "红烧肉", "price": null, "amount": 0.01, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "红烧肉", "price": null, "amount": 0.01, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "大米炒鸡蛋", "price": null, "amount": 0.66, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "红烧肉", "price": null, "amount": 0.02, "quantity": 2 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "大米炒鸡蛋", "price": null, "amount": 0.66, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "红烧肉", "price": null, "amount": 0.01, "quantity": 1 }] } }]vue怎么把里面的amount加一起获得总和
时间: 2023-06-28 17:12:18 浏览: 60
你可以通过遍历订单列表,累加每个订单的菜品总价来获得总金额。具体可以使用reduce方法,如下所示:
```
const orders = [ { "date": "2023-05-22", "orderList": [{ "id": 22, "schoolId": null, "schoolName": null, "name": null, "menuType": 1, "amount": 0.66, "refundAmount": null, "orderNo": null, "status": 3, "week": null, "orderDate": null, "detailList": [{ "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "大米炒鸡蛋", "price": null, "amount": 0.66, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "大米炒鸡蛋", "price": null, "amount": 0.66, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "红烧肉", "price": null, "amount": 0.01, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "大米炒鸡蛋", "price": null, "amount": 0.66, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "红烧肉", "price": null, "amount": 0.01, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "红烧肉", "price": null, "amount": 0.01, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "大米炒鸡蛋", "price": null, "amount": 0.66, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "红烧肉", "price": null, "amount": 0.02, "quantity": 2 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "大米炒鸡蛋", "price": null, "amount": 0.66, "quantity": 1 }, { "id": null, "detailNo": null, "menuType": null, "orderDate": "2023-05-22", "name": "红烧肉", "price": null, "amount": 0.01, "quantity": 1 }] } }];
const totalAmount = orders.reduce((acc, order) => {
const orderAmount = order.orderList.reduce((subAcc, item) => {
return subAcc + item.amount;
}, 0);
return acc + orderAmount;
}, 0);
console.log(totalAmount); // 输出总金额
```
在上面的代码中,我们首先使用reduce方法遍历订单列表,累加每个订单的菜品总价。每个订单的菜品总价是通过使用reduce方法遍历订单中的detailList属性来计算的。最后,使用reduce方法累加所有订单的菜品总价,得到了最终的总金额。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)