2016-11-30 79 views
1

如何将一个数组中的所有相同元素求和?例如,我有一个阵列:在一个数组中加上所有相同的元素

[20,20,20,10,10,5,1] 

我怎样才能使它[60,20,5,1]

这是我迄今为止尝试:

var money = [20, 20, 20, 10, 10, 5, 1]; 
for (var i = 0; i < money.length; i++) { 
    if (money[i] == money[i + 1]) { 
    money[i] += money[i + 1]; 
    money.splice(money.indexOf(money[i + 1]), 1); 
    } 
} 
+4

那你试试? – PMerlet

+1

你保证重复将会在一起吗?如果(货币[i] ==货币[i + 1]){ 货币[i] + =货币[i + 1];对于(var i = 0; i

+0

(money.indexOf(money [i + 1]),1); } } –

回答

2

使用Array#reduce方法与一个变量来存储以前的元件。

var arr = [20, 20, 20, 10, 10, 5, 1]; 
 
// variable for storing previous element 
 
var prev; 
 

 
var res = arr.reduce(function(arr, v) { 
 
    // if element is same as previous then add 
 
    // value with last element 
 
    if (prev == v) 
 
    arr[arr.length - 1] += v; 
 
    // else push and update prev variable 
 
    else 
 
    arr.push(prev = v) 
 
    // return the array refernece 
 
    return arr; 
 
    // set initial value as empty array for result 
 
}, []) 
 

 
console.log(res);


UPDATE:如果相同的值是不相邻的,然后使用一个目的是参考索引。

var arr = [20, 20, 20, 10, 10, 5, 1]; 
 
// object for refering index 
 
var ref = {}; 
 

 
var res = arr.reduce(function(arr, v) { 
 
    // check property is defined or not if 
 
    // defined update value at the index 
 
    if (ref.hasOwnProperty(v)) 
 
    arr[ref[v]] += v; 
 
    else { 
 
    // else add property to object and push element 
 
    ref[v] = arr.length; 
 
    arr.push(prev = v) 
 
    } 
 
    // return array reference 
 
    return arr; 
 
    // set initial value as empty array for result 
 
}, []) 
 

 
console.log(res);

4

我会做这样的事情:

  1. 计数的出现。
  2. 将该值与出现次数相乘。

片段

// Our original array. 
 
var arr = [20, 20, 20, 10, 10, 5, 1]; 
 
// Let's have a counts object that stores the counts. 
 
var counts = {}; 
 

 
// Loop through the array to get the counts. 
 
for (var i = 0; i < arr.length; i++) { 
 
    var num = arr[i]; 
 
    counts[num] = counts[num] ? counts[num] + 1 : 1; 
 
} 
 

 
// Have a final array. 
 
var fin = []; 
 
// Multiply the count with the values and push it to the final array. 
 
for (var count in counts) { 
 
    fin.push(counts[count] * count); 
 
} 
 

 
console.log(fin);

+0

解释此人 – Mahi

+0

@Mahi为什么你不能看到评论?哪一个你不明白? –

+0

谢谢你的答案,但我是一个年轻漂亮的开发者..我不理解这部分计数[num] =计数[num]?计数[num] + 1:1; –

0

你可以使用一个哈希表,并把结果存储槽的索引。这也适用于未分类的值。

var data = [20, 20, 20, 10, 10, 5, 1], 
 
    result = []; 
 

 
data.forEach(function (a) { 
 
    if (!(a in this)) { 
 
     this[a] = result.push(0) - 1; 
 
    } 
 
    result[this[a]] += a; 
 
}, Object.create(null)); 
 
    
 
console.log(result);

1
var list= [20,20,20,10,10,5,1]; 
var result=[]; 
//index of already added values 
var listOfIndex=[]; 
for(var i=0;i<list.length;i++){ 
if(listOfIndex.indexOf(i)>=0){ 
    continue; 
} 
var number=list[i]; 
for(var j=i+1;j<list.length;j++){ 
if(list[i]==list[j]){  
    number = number+list[j]; 
    listOfIndex.push(j);//push in this list the index of the value that has been added 
    } 
} 
result.push(number); 
} 
console.log(result); 
+0

nope,不是真的神 –

+0

看到我更新的解决方案。 – Alee

0

使用Array.prototype.reduce和存储正在创建的结果数组指数a hash table另一个单回路提案 - 将处理未排序过的输入。

请参见下面的演示:

var array = [20, 20, 20, 10, 10, 5, 1]; 
 

 
var result = array.reduce(function(hash){ 
 
    return function(p,c) { 
 
    if(c in hash) { 
 
     p[hash[c]] += c; 
 
    } else { 
 
     // store indices in the array 
 
     hash[c] = p.push(c) - 1; 
 
    } 
 
    return p; 
 
    }; 
 
}(Object.create(null)),[]); 
 

 
console.log(result);

相关问题