从一个数组中按组获取Sum,并将总和更新为另一个数组以获取相同的组名

时间:2017-07-27 11:53:53

标签: javascript jquery

我有两个数组

levelOne = [
  {Code: "A", Count: 0, Date: 2017-04-01,....},
  {Code: "A", Count: 0, Date: 2017-05-01,....},
  {Code: "B", Count: 0, Date: 2017-04-01,....},
  {Code: "B", Count: 0, Date: 2017-05-01,....}
];    
levelTwo = [
  {Code: "A", Count: 10, Date: 2017-04-01,....},
  {Code: "A", Count: 12, Date: 2017-05-01,....},
  {Code: "B", Count: 50, Date: 2017-04-01,....},
  {Code: "B", Count: 18, Date: 2017-05-01,....},
  {Code: "A", Count: 20, Date: 2017-05-01,....},
  {Code: "B", Count: 60, Date: 2017-04-01,....},
  {Code: "B", Count: 77, Date: 2017-05-01,....}
];

我想更新levelOne数组的Count,使用levelTwo数组中Count的总和,代码和日期相同。

到目前为止我做了什么?

我在levelOne上放了$ .each循环,但我被卡住了。

    $.each( levelOne , function( index, value ){
                  //I'm stuck
        $.each( levelTwo , function( idx, val ){
             if(value.Code==val.Code && value.Date == val.Date){
                  //I'm stuck
             }
                  //I'm stuck                
        });
    });

有什么建议吗?有没有更好的方法来实现它?

4 个答案:

答案 0 :(得分:1)

您可以使用散列表的非常前向的方法,首先迭代目标数组,然后迭代源数组进行计数。

通过迭代levelOne,哈希表中填充代码和日期值作为键,对象本身作为值填充。

迭代levelTwo将实际的Count添加到结果数组中。



var levelOne = [{ Code: "A", Count: 0, Date: '2017-04-01' }, { Code: "A", Count: 0, Date: '2017-05-01' }, { Code: "B", Count: 0, Date: '2017-04-01' }, { Code: "B", Count: 0, Date: '2017-05-01' }],
    levelTwo = [{ Code: "A", Count: 10, Date: '2017-04-01' }, { Code: "A", Count: 12, Date: '2017-05-01' }, { Code: "B", Count: 50, Date: '2017-04-01' }, { Code: "B", Count: 18, Date: '2017-05-01' }, { Code: "A", Count: 20, Date: '2017-05-01' }, { Code: "B", Count: 60, Date: '2017-04-01' }, { Code: "B", Count: 77, Date: '2017-05-01' }],
    getKey = function (o) { return ['Code', 'Date'].map(function (k) { return o[k]; }).join('|'); },
    hash = Object.create(null);

levelOne.forEach(function (o) { hash[getKey(o)] = o; });
levelTwo.forEach(function (o) { hash[getKey(o)].Count += o.Count; });
   
console.log(levelOne);

.as-console-wrapper { max-height: 100% !important; top: 0; }




ES6与Map



var levelOne = [{ Code: "A", Count: 0, Date: '2017-04-01' }, { Code: "A", Count: 0, Date: '2017-05-01' }, { Code: "B", Count: 0, Date: '2017-04-01' }, { Code: "B", Count: 0, Date: '2017-05-01' }],
    levelTwo = [{ Code: "A", Count: 10, Date: '2017-04-01' }, { Code: "A", Count: 12, Date: '2017-05-01' }, { Code: "B", Count: 50, Date: '2017-04-01' }, { Code: "B", Count: 18, Date: '2017-05-01' }, { Code: "A", Count: 20, Date: '2017-05-01' }, { Code: "B", Count: 60, Date: '2017-04-01' }, { Code: "B", Count: 77, Date: '2017-05-01' }],
    hash = new Map,
    getKey = o => ['Code', 'Date'].map(k => o[k]).join('|');

levelOne.forEach(o => hash.set(getKey(o), o));
levelTwo.forEach(o => hash.get(getKey(o)).Count += o.Count);
   
console.log(levelOne);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:0)

levelOne = [
  {Code: "A", Count: 0, Date: '2017-04-01'},
  {Code: "A", Count: 0, Date: '2017-05-01'},
  {Code: "B", Count: 0, Date: '2017-04-01'},
  {Code: "B", Count: 0, Date: '2017-05-01'}
];    
levelTwo = [
  {Code: "A", Count: 10, Date: '2017-04-01'},
  {Code: "A", Count: 12, Date: '2017-05-01'},
  {Code: "B", Count: 50, Date: '2017-04-01'},
  {Code: "B", Count: 18, Date: '2017-05-01'},
  {Code: "A", Count: 20, Date: '2017-05-01'},
  {Code: "B", Count: 60, Date: '2017-04-01'},
  {Code: "B", Count: 77, Date: '2017-05-01'}
];

$.each( levelOne , function( index, value ){
    $.each( levelTwo , function( idx, val ){
         if(value.Code==val.Code && value.Date == val.Date){

               //just increment the value of your levelOne array here.
               value.Count += val.Count;
               //~or~
               //levelOne[index].Count += val.count;

         } 
    });
});

//example - just prints the results to a div
$.each( levelOne , function( index, value ){
    $("#results").append(value.Code + " - " + value.Count + " - " + value.Date + "<br />");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div id="results"></div>

答案 2 :(得分:0)

我认为您需要一个辅助数组来存储添加的计数:

var helper = new Array();
for(var i = 0; i < levelTwo.length; i++) {

    var entry = levelTwo[i];
    helper[entry.Code][entry.Date] += entry.Count;

}

然后再次循环通过辅助数组,将值放入levelOne

修改

竞争示例:

var levelOne = new Array(
  {Code: "A", Count: 0, Date: '2017-04-01'},
  {Code: "A", Count: 0, Date: '2017-05-01'},
  {Code: "B", Count: 0, Date: '2017-04-01'},
  {Code: "B", Count: 0, Date: '2017-05-01'}
);    
var levelTwo = new Array(
  {Code: "A", Count: 10, Date: '2017-04-01'},
  {Code: "A", Count: 12, Date: '2017-05-01'},
  {Code: "B", Count: 50, Date: '2017-04-01'},
  {Code: "B", Count: 18, Date: '2017-05-01'},
  {Code: "A", Count: 20, Date: '2017-05-01'},
  {Code: "B", Count: 60, Date: '2017-04-01'},
  {Code: "B", Count: 77, Date: '2017-05-01'}
);


var helper = new Array();
for(var i = 0; i < levelTwo.length; i++) {

    if(typeof helper[levelTwo[i].Code] == 'undefined') {
        console.log('reset ' + levelTwo[i].Code);
        helper[levelTwo[i].Code] = new Array();
    }
    if(typeof helper[levelTwo[i].Code][levelTwo[i].Date] == 'undefined') {
        helper[levelTwo[i].Code][levelTwo[i].Date] = 0;
    }
    helper[levelTwo[i].Code][levelTwo[i].Date] += levelTwo[i].Count;
}


for(var j=0; j<levelOne.length;j++) {
    if(helper[levelOne[j].Code][levelOne[j].Date]) {
        levelOne[j].Count = helper[levelOne[j].Code][levelOne[j].Date];
    }
    else {
        levelOne[j].Count = 0;
    }
}

注意: levelTwo数组不应该包含对leverOne

中非现有元素的引用

答案 3 :(得分:0)

尝试此代码 -

for(i = 0; i < levelOne.length; i++){
    for(var  j = 0; j < levelTwo.length; j++) {
        if(levelOne[i]['Code'] == levelTwo[j]['Code'] && levelOne[i]['Date'] == levelTwo[j]['Date']){
            levelOne[i]['Count'] += levelTwo[j]['Count'];
        }
    }
}
console.log(levelOne);