2016-11-30 78 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);

相關問題