从两个数组求和值

时间:2015-09-25 07:51:17

标签: javascript jquery

我有两个动态创建的数组。两者中的单元格数相同(array.length相同,类似于键和值关联)。他们在这里:

barData.labels["Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Phone"]

barData.datasets[0].data["2", "8", "20", "200", "1", "300", "400", "500", "77", "7", "99"]

我需要检查barData.labels中是否存在重复值,如果是,我需要对barData.datasets[0].data中的值求和并删除它们。例如,如果有2个Food条目,我需要对值进行求和,然后删除其中一个。

我怎样才能做到这一点?

6 个答案:

答案 0 :(得分:3)

也许你可以在这里使用Javascript对象,而不是使用两个数组。此外,在创建时执行此操作可能会更有效。

你会有这样的事情:

barData = {
  "Food": ["2", "8", "20", "200", "1", ...]
}

因此,如果您必须插入另一个Food键,则可以检查对象Food中是否已定义barData,如果没有,则创建它,否则您只需添加数组中与Food键对应的元素。

答案 1 :(得分:0)

您可以使用数组。过滤器功能

看看这里

jQuery function to get all unique elements from an array?

答案 2 :(得分:0)

让我们考虑您有两个如下所示的数组:

$('#close').click(function() {
    $(".wrapper").hide();
});

现在你需要一个如下输出:

var labels = ["Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Phone", "Phone", "Status", "Status"];
var barData = ["2", "8", "20", "200", "1", "300", "400", "500", "77", "7", "99", "1", "333", '77'];

以下代码将执行此操作:

var result = {
  "Food": ["2","8","20","200","1","300","400","500","77","7"]
  "Phone": ["99","1"],
  "Status": ["333","77"]
}

如果你想总结它们,那么你可以改变一下,如下所示:

var tempArr = []
var result  = {};
for(index in labels){
   var elm    = labels[index];
   if(tempArr.indexOf(elm) > -1){
      result[elm].push(barData[index])
   }
   else{
      tempArr.push(elm);
      result[elm] = [barData[index]];
   }
}
console.log(result);

答案 3 :(得分:0)

这是使用地图跟踪重复项的简单应用,因此您知道何时添加以及在何处进行求和。

这样的事情:

var ls = [ ... ];
var vs = [ ... ];
var dups = {};
var lls = [];
var vvs = [];
for(var i = 0, len = vs.length; i < len; i++) {
    var l = ls[i]; 
    var v = vs[i];
    if(dups.hasOwnProperty(l)) {
        var ii = dups[l];
        vvs += vs[ii];
    } else {
        var ii = vvs.length;
        dups[l] = ii;
        lls[ii] = l;
        vvs[ii] = v;
    }
}

答案 4 :(得分:0)

您可以做的是将您的值添加到临时对象,随时将值相加,然后将其转换回原始数组的格式。

&#13;
&#13;
// Creating your original array
var barData = {};
barData.labels = ["Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Phone"];
barData.datasets = [];
barData.datasets[0] = {};
barData.datasets[0].data = ["2", "8", "20", "200", "1", "300", "400", "500", "77", "7", "99"];
// Display original object
$("#original").text(JSON.stringify(barData));
var newBarData = {};
for (var i = 0; i < barData.labels.length; i++) {
  if (newBarData[barData.labels[i]] === undefined) {
    newBarData[barData.labels[i]] = {};
    newBarData[barData.labels[i]].labels = barData.labels[i];
    newBarData[barData.labels[i]].value = parseInt(barData.datasets[0].data[i]);
  } else
    newBarData[barData.labels[i]].value = newBarData[barData.labels[i]].value + parseInt(barData.datasets[0].data[i]);
}
//Convert back to original format
barData = {};
barData.labels = [];
barData.datasets = [];
barData.datasets[0] = {};
barData.datasets[0].data = [];
$.each(newBarData, function(index, me) {
  barData.labels.push(me.labels);
  barData.datasets[0].data.push(me.value);
});

$("#result").text(JSON.stringify(barData));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="original"></div>
<div id="result"></div>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

我建议使用项目的分布构建一个对象,然后重新创建标签和计数。

var labels = ["Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Phone"],
    data = ["2", "8", "20", "200", "1", "300", "400", "500", "77", "7", "99"],
    distribution = labels.reduce(function (r, a, i) {
        r[a] = (r[a] || 0) + +data[i];
        return r;
    }, {});

Object.keys(distribution).forEach(function (k, i) {
    if (!i) {
        labels = [];
        data = [];
    }
    labels.push(k);
    data.push(distribution[k]);
});

document.write('<pre>' + JSON.stringify(labels, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');