【问题标题】:How Do I loop this arrays to extract only the orderTotal?如何循环此数组以仅提取 orderTotal?
【发布时间】:2018-02-22 16:26:18
【问题描述】:

如何循环这个数组并在 Item 对象中添加 orderTotal 字段? (0) OrderTotal $100 (1) OrderTotal $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"
    }

【问题讨论】:

标签: javascript jquery arrays loops object


【解决方案1】:

你可以像这样减少数组:

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

【讨论】:

  • 这是否是来自 API 的响应是否重要?
  • 不,只要你减少的是一个数组就可以了
  • 重复的答案,只有 2 分钟,显然是无意的
  • @BrunoPaula 为什么这很重要?数据就是数据,不管它来自哪里。
【解决方案2】:
var sum = myArray.reduce((sum, current) => sum + current.orderTotal, 0);

【讨论】:

  • 虽然答案总是很受欢迎,但提供一些有关如何您的代码解决手头问题的附加信息确实很有帮助。这样做有助于那些有类似(但不相同)问题的人。不是每个人都熟悉您的确切编码逻辑,但可能了解您的一般方法概念。为了帮助改进您的答案,请提供一些context surrounding it,并查看writing great answers 上的帮助文章以获取有关如何使您的答案重要的一些提示:)
【解决方案3】:

使用Array#map提取所有orderTotal。

然后,使用Array#reduce 对所有 orderTotal 求和。

const array = [{
    "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"
    }
  }
];
console.log('map result ', array.map(item => item.item.orderTotal));
console.log('reduce on map result ', array.map(item => item.item.orderTotal).reduce((item1, item2) => (item1 + item2)));

【讨论】:

  • 这是否是来自 API 的响应是否重要?
  • #reduce 从 es5.1 开始存在,并且与 IE9 兼容。仅当您想要 IE
【解决方案4】:

您可以使用jquery.each 语句来跟踪这个数组。 浏览下面的演示。

var array=[
          {
            "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"
            }
            }];
  var sum=0;
$.each( array, function( key, itemData ) {
  console.log("orderTotal: "+ itemData.item.orderTotal);
  sum+=itemData.item.orderTotal;
});
 console.log(sum);           
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

【讨论】:

  • 这是您的预期输出吗?
猜你喜欢
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 2017-01-27
  • 2015-05-14
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
相关资源
最近更新 更多