使用javascript简化对象计算

时间:2016-02-08 20:27:46

标签: javascript json algorithm

我已将JSON对象保存在变量中,并且我正在对象中的属性进行计算;但是,我发现写入的计算时间很长,如果数据变大,计算可能会变得很长。

我希望有一种更简单的方法来进行计算,而不是我目前正在进行的计算。

以下是javascript文件中保存的JSON数据:

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":null,
    "Sprint_7":null
  }],
  "Bugs_In_Progress": [{
    "Sprint_2":null,
    "Sprint_3":null,
    "Sprint_4":7,
    "Sprint_5":4,
    "Sprint_6":null,
    "Sprint_7":null
  }]
};

这是我用来计算各种键中数字的计算:

var totDone = obj.Ready_for_Test[0].Sprint_2 + obj.Ready_for_Test[0].Sprint_3 + obj.Ready_for_Test[0].Sprint_4 + obj.Ready_for_Test[0].Sprint_5 + obj.Ready_for_Test[0].Sprint_6 + obj.Ready_for_Test[0].Sprint_7 +
    obj.Test[0].Sprint_2 + obj.Test[0].Sprint_3 + obj.Test[0].Sprint_4 + obj.Test[0].Sprint_5 + obj.Test[0].Sprint_6 + obj.Test[0].Sprint_7 +
    obj.Ready_for_Acceptance[0].Sprint_2 + obj.Ready_for_Acceptance[0].Sprint_3 + obj.Ready_for_Acceptance[0].Sprint_4 + obj.Ready_for_Acceptance[0].Sprint_5 + obj.Ready_for_Acceptance[0].Sprint_6 + obj.Ready_for_Acceptance[0].Sprint_7 +
    obj.Accepted[0].Sprint_2 + obj.Accepted[0].Sprint_3 + obj.Accepted[0].Sprint_4 + obj.Accepted[0].Sprint_5 + obj.Accepted[0].Sprint_6 + obj.Accepted[0].Sprint_7;
    console.log(totDone); 

如果我的JSON数据扩展,为了进行进一步的计算,我的算法将变得非常冗长。有没有更简单的方法来运行数据计算?

3 个答案:

答案 0 :(得分:0)

您可以像这样致电getSum

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;
}

并获得所有

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

      total += getSum(onj, key);
 }

console.log(total);

如果您只想添加一些特定的sprint值,那么您需要传递一些sprint名称列表,如

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']);

答案 1 :(得分:0)

假设您只想总结该JSON中的所有非空值,这里是一个带for循环的简单解决方案: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);

答案 2 :(得分:0)

首先,数据结构很重要。结合评论部分中的所有建议和我自己的经验,我建议按如下方式构建数据。

// 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
    ]
};

首先要注意的是,语法字符({,},[,],:,'和“)的需要量要少得多。这不一定是代码质量的指标,但在这种情况下它也大大简化了下一组代码。

要获得总和,您可以使用Array.prototype.reduce()Array.prototype.map()等函数迭代数据,并以最少的重复执行强大的计算。

// 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 previousValue + currentValue;
    }
  });
};

// Finds the sums of the arrays for each stage you want to add up
var postDevBacklogStageSums = postDevBacklogStages.map(function(currentValue, index, array){
    return sumArray(currentValue);
});

// Finds the sum of the sums of stages
var sumTotal = sumArray(postDevBacklogStageSums);

console.log(postDevBacklogStageSums); // [8, 11, 3, 157]
console.log(sumTotal); // 179