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



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

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

当删除重复项时,必须将其值添加到唯一值

试试这个

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

这是一个简单的解决方案,不是很复杂,但可以完成任务。

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

这段代码可能会有所帮助。

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

您可以使用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);

在这里,我们首先比较索引值,然后找出重复元素的总和-

  function sumOfRepeatedElements(numbers){
    let repeatedElementsArr = [];
    for(let i = 0; i < numbers.length; i++){
        if(numbers.indexOf(numbers[i]) !== numbers.lastIndexOf(numbers[i])){
            repeatedElementsArr.push(numbers[i])
        }
    }
    const sumArr = [...new Set(repeatedElementsArr)];
    return sumArr.reduce((acc,curr) => acc+curr,0)
}
console.log(sumOfRepeatedElements([1,2,3,4,3,5,6,5,8]));    // 3+5=8
console.log(sumOfRepeatedElements([2,2,3,4,3,5,6,5,7]));    // 2+3+5=10

相关内容

  • 没有找到相关文章

最新更新