justinrian February 2016

Simplify object calculations using javascript

I have saved my JSON object in a variable and I am making calculations off of the properties within the object; however, I am finding that the calculations are very long to write and if the data becomes larger, the calculations can become pretty long.

I am hoping that there is a simpler way to make calculations than how I am currently doing it.

Here is the JSON data saved within a javascript file:

var obj = {
  "Open": [{
    "Sprint_2":null,
    "Sprint_3":null,
    "Sprint_4":null,
    "Sprint_5":6,
    "Sprint_6":38,
    "Sprint_7":7
  }],
  "Design": [{
    "Sprint_2":null,
    "Sprint_3":null,
    "Sprint_4":null,
    "Sprint_5":null,
    "Sprint_6":1,
    "Sprint_7":null
  }],
  "Requirement": [{
    "Sprint_2":null,
    "Sprint_3":null,
    "Sprint_4":null,
    "Sprint_5":1,
    "Sprint_6":1,
    "Sprint_7":null
  }],
  "Ready_for_Build": [{
    "Sprint_2":null,
    "Sprint_3":null,
    "Sprint_4":null,
    "Sprint_5":4,
    "Sprint_6":2,
    "Sprint_7":null
  }],
  "Build": [{
    "Sprint_2":null,
    "Sprint_3":null,
    "Sprint_4":null,
    "Sprint_5":12,
    "Sprint_6":1,
    "Sprint_7":null
  }],
  "Ready_for_Test": [{
    "Sprint_2":null,
    "Sprint_3":null,
    "Sprint_4":null,
    "Sprint_5":4,
    "Sprint_6":4,
    "Sprint_7":null
  }],
  "Test": [{
    "Sprint_2":null,
    "Sprint_3":null,
    "Sprint_4":null,
    "Sprint_5":5,
    "Sprint_6":6,
    "Sprint_7":null
  }],
  "Ready_for_Acceptance": [{
    "Sprint_2":null,
    "Sprint_3":null,
    "Sprint_4":null,
    "Sprint_5":3,
    "Sprint_6":null,
    "Sprint_7":null
  }],
  "Accepted": [{
    "Sprint_2":38,
    "Sprint_3":43,
    "Sprint_4":57,
    "Sprint_5":19,
    "Sprint_6":null,
    "Sprint_7":null
  }],
  "Total_Bugs": [{
    "Sprint_2":47,
    "Sprint_3":39,
    "Sprint_4":71,
    "Sprint_5":39,
    "Sprint_6":null,
    "Sprint_7":null
  }],
  "Bugs_Success": [{
    "Sprint_2":37,
    "Sprint_3":25,
    "Sprint_4":42,
    "Sprint_5":11,
    "Sprint_6":        

Answers


Zohaib Ijaz February 2016

You can call getSum like this

var answer = getSum(obj, 'Test');
console.log(answer);

function getSum(data, key){
    var sprints = data[key][0];
    var sum = 0;
    for (var sprint in sprints){
         if(sprints[sprint]){
              sum += sprints[sprint];
         }
    }
    return sum;
}

and to get total of all

 var total = 0;
 for (var key in obj){

      total += getSum(onj, key);
 }

console.log(total);

if you just want to add some specific sprint values, then you need to pass a list of those sprint names like

function getSum(data, key, sprintNames){
    var sprints = data[key][0];
    var sum = 0;
    sprintNames.forEach(function(name){
        if(sprints[name]){
              sum += sprints[name]
         }
    });
    return sum;
}

getSum(obj, 'Design', ['Sprint_2', 'Sprint_3']);


alumarcu February 2016

Assuming you just want to sum up all non-null values in that JSON, here's a simple solution with for loops: https://jsfiddle.net/vgpw4h1h/1/

var sum = 0;
for (status in obj) {
  for (project in obj[status]) {
    for (sprint in obj[status][project]) {
      sum += obj[status][project][sprint];
    }
  }
}
console.log('Points from all sprints: ' + sum);


Simon Merrick February 2016

Firstly, data structure is important. Combining all of the suggestions in the comments section and my own experience, I would suggest structuring the data as follows.

// Your JavaScript Object
var backlog = {
    Open: [
        null, null, null,
        6, 38, 7
    ],
    Design: [
        null, null, null,
        null, 1, null
    ],
    Requirement: [
        null, null, null,
        1, 1, null
    ],
    Ready_for_Build: [
        null, null, null,
        4, 2, null
    ],
    Build: [
        null, null, null,
        12, 1, null
    ],
    Ready_for_Test: [
        null, null, null,
        4, 4, null
    ],
    Test: [
        null, null, null,
        5, 6, null
    ],
    Ready_for_Acceptance: [
        null, null, null,
        3, null, null
    ],
    Accepted: [
        38, 43, 57,
        19, null, null
    ],
    Total_Bugs: [
        47, 39, 71,
        39, null, null
    ],
    Bugs_Success: [
        37, 25, 42,
        11, null, null
    ],
    Bugs_In_Progress: [
        null, null, 7,
        4, null, null
    ]
};

First note how greatly fewer syntactical characters ({, }, [, ], :, ', and ") are required. This isn't necessarily an indicator of code quality but in this case it also greatly simplifies the next set of code.

To get your sums you can use functions such as Array.prototype.reduce() and Array.prototype.map() to iterate over the data and perform powerful calculations with minimal repetition.

// A collection of the stages that you want to add up
var postDevBacklogStages = [
    backlog.Ready_for_Test,
    backlog.Test,
    backlog.Ready_for_Acceptance,
    backlog.Accepted
];

// A helper funnction to find the sum of an array
var sumArray = function (array){
    return array.reduce(function(previousValue, currentValue, currentIndex, array){
    if (typeof(currentValue) != "number") {
        return previousValue;
    } else {
      return previousVa 

Post Status

Asked in February 2016
Viewed 3,457 times
Voted 4
Answered 3 times

Search




Leave an answer