Home » Javascript » Sum the values within the JSON Array having Array Of Objects

Sum the values within the JSON Array having Array Of Objects

Posted by: admin November 1, 2017 Leave a comment

Questions:

I have below JSON Array and each Array Contains the Array Of Objects and Now I am want to Sum the values of the Array Objects,

Before Downvoting, I am asking this question to get the output considering the performance issue as I have to perform the operation on Array Of length of 1Million

Can I achieve this easily in underscore or anything other simpler way

In the Below JSON Data,

I want to get the Total Pass Count, FAIL Count and UNEXECUTED Count and Total Count

OutPut Expecting

Project ID : 12151
Pass Count : 7
Fail Count : 3
UNEXECUTED Count : 9
totalExecutions : 19

Project ID : 12152
Pass Count : 8
Fail Count : 10
UNEXECUTED Count : 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
  }
]

Below is my approach to find the value, let me know how to make it optmized or even faster

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);
            }); 
        });
    });
}
Answers: