gpt4 book ai didi

javascript - 如何循环该数组以仅提取 orderTotal?

转载 作者:行者123 更新时间:2023-12-03 03:28:24 25 4
gpt4 key购买 nike

如何循环该数组并在 Item 对象内添加 orderTotal 字段? (0) 订单总计 100 美元 (1) 订单总计 220 美元

var sum = 320;

将两个 OrderTotal 字段的总和保存到变量

我的目标是获得所有订单总计字段的总和

[
{
"itemID": "C619D69C-970D-11E7-AFD4-E44BB50029CC",
"itemType": "Job",
"item": {
"jobColor": "18, 2, 218",
"jobType": "Work",
"employeeIDs": [
"af879a7d-1aec-e111-834a-002590274606"
],
"note": "",
"jobStatus": "Scheduled",
"actualDateTimeStart": "9/13/2017 11:00 AM",
"orderTotal**": 100,
"orderRefNumber": "",
"orderNumber": 8375,
"orderType": "Estimate",
"siteID": "56f6f468-957c-11e7-b464-b4bf180545bb"
}
},
{
"itemID": "F9CF5384-9244-11E7-9BCA-E81C6A1CA1CB",
"itemType": "Job",
"item": {
"jobColor": "255, 126, 0",
"jobType": "Work",
"employeeIDs": [
"af879a7d-1aec-e111-834a-002590274606"
],
"note": "",
"jobStatus": "Scheduled",
"actualDateTimeStart": "9/13/2017 2:00 PM",
"orderGroup": "",
"orderTotal": 220,
"orderRefNumber": "",
"orderNumber": 8367,
"orderType": "Work Order",
"siteID": "7e131f90-6657-11e7-8416-875831a581c3"
}

最佳答案

你可以像这样减少数组:

const sum = orders.reduce((acc, order) => acc + order.orderTotal, 0);

关于javascript - 如何循环该数组以仅提取 orderTotal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46206347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com