比较数组中的值以计算它们并合并表中的行

时间:2019-06-22 07:03:52

标签: javascript jquery arrays angularjs

我的代码中有一个搜索字段,用户可以在其中搜索有关另一个ID为ID的用户的信息。搜索完成后,系统将返回$scope.mixedList数组。这是动态数组,输出可能不同。

我需要检查:

if(cat_id == previous_cat_id && process_id == previous_process_id){
  summarize output;
  summarize achievement;
  merge rows in table;
}else{
  output = output;
  achievement = achievement;
  do not merge rows in table;
}

为此,我创建了一个函数calculateCategory

var cat_output = 0;
var cat_achievement = 0;
function calculateCategory(list) {
  if (list.length > 0) {
    list[0].OutAchCatRowMatch=false;
    for (var i = 0; i < list.length; i++) {
      var process_id = list[i].process_id;
      var cat_id = list[i].cat_id;
      var output = list[i].output;
      var achievement = list[i].achievement;

      var outp_rows = 1;
      for (var j = i + 1; j < list.length; j++) {
        if (process_id === list[j].process_id && cat_id === list[j].cat_id) {
          outp_rows++;
          cat_output = parseFloat(list[j].output) + parseFloat(output);
          cat_achievement = parseFloat(list[j].achievement) + parseFloat(achievement);
          list[j].OutAchCatRowMatch=true;
        }else{              
          cat_output = parseFloat(list[j].output);
          cat_achievement = parseFloat(list[j].achievement);  
          list[j].OutAchCatRowMatch=false;
          break;
        }
      }
      list[i].cat_output = cat_output;
      list[i].cat_achievement = cat_achievement;
      list[i].outp_rows = outp_rows;
    }
  }
}

这是数组输出和html表输出的两个示例:

第一个结果应该是合并的行,并且第一个合并的行不正确:

$scope.mixedList = [
    { pcode: "ENC2", cat_id: "5", process_id: "4", output: "102", achievement: 33.55 },
    { pcode: "LMA", cat_id: "5", process_id: "4", output: "11", achievement: 3.62 },
    { pcode: "ENC2", cat_id: "5", process_id: "3", output: "92", achievement: 28.22 },
    { pcode: "LMA", cat_id: "5", process_id: "3", output: "11", achievement: 3.37 },
];

First row is wrong here

因为cat_idprocess_id不同,所以不必合并第二个数组。这只是一团糟:

$scope.mixedList = [
    { pcode: "LMA KEY", cat_id: "7", process_id: "5", output: "5.13", achievement: 1.95 },
    { pcode: "TFY", cat_id: "8", process_id: "5", output: "50.64", achievement: 21.28 },
    { pcode: "LMA INB", cat_id: "7", process_id: "6", output: "5.13", achievement: 1.91 },
];

enter image description here

我的功能出了什么问题?如何更改它才能使其正常工作?

1 个答案:

答案 0 :(得分:3)

代码中的问题是else语句。无论是否发生合并,它的行为都必须有所不同。

我修改了代码,以使用下面的Map函数(根据我对代码的理解)来缓存计算的数据,然后使用它。让我知道是否有帮助。

function calculateCategory(list) {
  var cache = new Map();
  if (list.length > 0) {
    list[0].OutAchCatRowMatch = false;
    var merge = false;
    for (var i = 0; i < list.length; i++) {
      var process_id = list[i].process_id;
      var cat_id = list[i].cat_id;
      var cat_output = list[i].output;
      var cat_achievement = list[i].achievement;
      var outp_rows = 1;

      var key = list[i].process_id + ":" + list[i].cat_id;
      if (cache.has(key)) {
        var cachedValues = cache.get(key);
        list[i].cat_output = cachedValues.cat_output;
        list[i].cat_achievement = cachedValues.cat_achievement;
        list[i].outp_rows = cachedValues.outp_rows;
      } else {
        for (var j = i + 1; j < list.length; j++) {
          if (process_id === list[j].process_id && cat_id === list[j].cat_id) {
            outp_rows++;
            cat_output = parseFloat(list[j].output) + parseFloat(cat_output);
            cat_achievement = parseFloat(list[j].achievement) + parseFloat(cat_achievement);
            list[j].OutAchCatRowMatch = true;
          }
        }
        cache.set(key, {
          "cat_output": cat_output,
          "cat_achievement": cat_achievement,
          "outp_rows": outp_rows
        });
        list[i].cat_output = cat_output;
        list[i].cat_achievement = cat_achievement;
        list[i].outp_rows = outp_rows;
      }
    }
  }
}