对具有对象数组的JSON数组中的值求和

时间:2017-10-26 18:55:56

标签: javascript json underscore.js

我有JSON数组,每个数组都包含对象数组,现在我想要对数组对象的值求和,

Downvoting 之前,我问这个问题是考虑到性能问题得到输出,因为我必须对长度为1M的数组执行操作

我可以用下划线或任何其他更简单的方式轻松实现这一目标

在JSON数据下面,

我想获得总通过次数,失败次数和未完成次数和总计数

OutPut Expecting

项目ID:12151 通过数:7 失败数:3 未完成的数量:9 totalExecutions:19

项目ID:12152 通过数:8 失败数:10 未完成的数量:2 totalExecutions:20

[
  {
    "projectId": 12151,
    "PASS": 7,
    "FAIL": 3,
    "UNEXECUTED": 9,
    "TOTAL": 19
  },
  {
    "projectId": 12152,
    "PASS": 8,
    "FAIL": 10,
    "UNEXECUTED": 2,
    "TOTAL": 20
  }
]

INPUT JSON

[
  {
    "4106": {
      "totalExecutions": 11,
      "projectId": 12151,
      "executionSummaries": {
        "executionSummary": [
          {
            "count": 3,
            "statusName": "UNEXECUTED"
          },
          {
            "count": 5,
            "statusName": "PASS"
          },
          {
            "count": 3,
            "statusName": "FAIL"
          }
        ]
      }
    },
    "4107": {
      "totalExecutions": 8,
      "projectId": 12151,
      "executionSummaries": {
        "executionSummary": [
          {
            "count": 6,
            "statusName": "UNEXECUTED"
          },
          {
            "count": 2,
            "statusName": "PASS"
          },
          {
            "count": 0,
            "statusName": "FAIL"
          }
        ]
      }
    },
    "recordsCount": 2
  },
  {
    "4047": {
      "totalExecutions": 9,
      "projectId": 12152,
      "executionSummaries": {
        "executionSummary": [
          {
            "count": 0,
            "statusName": "UNEXECUTED"
          },
          {
            "count": 3,
            "statusName": "PASS"
          },
          {
            "count": 6,
            "statusName": "FAIL"
          }
        ]
      }
    },
    "4048": {
      "totalExecutions": 6,
      "projectId": 12152,
      "executionSummaries": {
        "executionSummary": [
          {
            "count": 2,
            "statusName": "UNEXECUTED"
          },
          {
            "count": 2,
            "statusName": "PASS"
          },
          {
            "count": 2,
            "statusName": "FAIL"
          }
        ]
      }
    },
    "4049": {
      "totalExecutions": 5,
      "projectId": 12152,
      "executionSummaries": {
        "executionSummary": [
          {
            "count": 0,
            "statusName": "UNEXECUTED"
          },
          {
            "count": 3,
            "statusName": "PASS"
          },
          {
            "count": 2,
            "statusName": "FAIL"
          }
        ]
      }
    },
    "recordsCount": 3
  }
]

以下是我找到价值的方法,让我知道如何使其优化或更快

function findtheTotal() {
    return getTheLiveDayUpdates().then(function (items) {
        var TotalCount = 0;
        angular.forEach(items, function (item) {
            angular.forEach(item, function (eachValue) {
                TotalCount = TotalCount + eachValue.totalExecutions;
               console.log("Total", TotalCount);
            }); 
        });
    });
}

0 个答案:

没有答案