如何使用javascript在数组中查找重复值的总和

时间:2016-10-15 06:03:31

标签: javascript arrays multidimensional-array

我和我有以下数组。我的目标是通过从该数组中删除重复值来创建一个新数组。

var xDataValuesdash =  [    [ 'Anantapur', 10, '#C21466' ],
                            [ 'Anantapur', 20, '#A983D0' ],
                            [ 'Chittoor', 30, '#A993D0' ],
                            [ 'Anantapur', 30, '#544DDF' ],
                            [ 'Anantapur', 4, '#A994D0' ],
                            [ 'Chittoor', 40, '#544BDF' ]  ];

即,结果数组必须是

var new_xDataValuesdash = [[ 'Anantapur', 64, '#C21466' ], 
                           [ 'Chittoor', 70, '#544BDF' ]];

删除重复项时,必须将其值添加到unqiue值。

4 个答案:

答案 0 :(得分:4)

试试这个



var xDataValuesdash =  [    [ 'Anantapur', 10, '#C21466' ],
    [ 'Anantapur', 20, '#A983D0' ],
    [ 'Chittoor', 30, '#A993D0' ],
    [ 'Anantapur', 30, '#544DDF' ],
    [ 'Anantapur', 4, '#A994D0' ],
    [ 'Chittoor', 40, '#544BDF' ]  ];

var sum = {},result;

for (var i=0,c;c=xDataValuesdash[i];++i) {
    if ( undefined === sum[c[0]] ) {        
       sum[c[0]] = c;
    }
    else {
        sum[c[0]][1] += c[1];
    }
}
result = Object.keys(sum).map(function(val) { return sum[val]});

alert(JSON.stringify(result));




答案 1 :(得分:1)

这是一个简单的解决方案,不是非常复杂,但可以完成工作。

var a = [
    ['Anantapur', 10, '#C21466'],
    ['Anantapur', 20, '#A983D0'],
    ['Chittoor', 30, '#A993D0'],
    ['Anantapur', 30, '#544DDF'],
    ['Anantapur', 4, '#A994D0'],
    ['Chittoor', 40, '#544BDF']
];

var findDuplicatesAndSum = function(inptArr) {
    var duplicateIndex = {};
    var outputArr = [];
    for (var i = 0; i < inptArr.length; i++) {
        var item = inptArr[i];
        var collisionIndex = duplicateIndex[item[0]];
        if (collisionIndex > -1) {
            outputArr[collisionIndex][1] += item[1];
        } else {
            outputArr.push(item);
            duplicateIndex[item[0]] = outputArr.length - 1;
        }
    }
    console.log(outputArr);
    return outputArr;
};

findDuplicatesAndSum(a);

答案 2 :(得分:0)

此代码可能会有所帮助。

&#13;
&#13;
var xDataValuesdash =  [    
	[ 'Anantapur', 10, '#C21466' ],
    [ 'Anantapur', 20, '#A983D0' ],
    [ 'Chittoor', 30, '#A993D0' ],
    [ 'Anantapur', 30, '#544DDF' ],
    [ 'Anantapur', 4, '#A994D0' ],
    [ 'Chittoor', 40, '#544BDF' ]  ];
var new_DataValuesdash = [];
for(var i=0; i < xDataValuesdash.length;i++){
	if(new_DataValuesdash[xDataValuesdash[i][0]] == undefined) {
        new_DataValuesdash[xDataValuesdash[i][0]] = xDataValuesdash[i];
    }
    else {
        // new_DataValuesdash[xDataValuesdash[i][0]] = [];
        new_DataValuesdash[xDataValuesdash[i][0]][1] = new_DataValuesdash[xDataValuesdash[i][0]][1] + xDataValuesdash[i][1];
    }
}
console.log(new_DataValuesdash);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以使用for..of循环,Array.prototype.some()Array.prototype.forEach()

var xDataValuesdash =  [    [ 'Anantapur', 10, '#C21466' ],
                            [ 'Anantapur', 20, '#A983D0' ],
                            [ 'Chittoor', 30, '#A993D0' ],
                            [ 'Anantapur', 30, '#544DDF' ],
                            [ 'Anantapur', 4, '#A994D0' ],
                            [ 'Chittoor', 40, '#544BDF' ]  ];


var res = [];
for (var prop of xDataValuesdash) {
  !res.some(value => value && value[0] === prop[0])
  ? res.push(prop)
  : res.forEach(value => {
      if (value[0] === prop[0]) value[1] += prop[1]
    });      
}

console.log(res);