2016-11-30 5 views
1

Wie kann ich alle Elemente in einem Array zusammenfassen? Zum Beispiel habe ich ein Array:Summe aller Elemente in einem Array

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

Wie kann ich es machen [60,20,5,1]?

Hier ist, was ich versucht habe, so weit:

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

Was haben Sie versucht? – PMerlet

+1

Sind Sie sicher, dass die Duplikate zusammen sind? –

+0

für (var i = 0; i

Antwort

2

Verwenden Array#reduce Verfahren mit einem variablen vorhergehenden Element zu speichern.

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: Wenn dieselben Werte sind dann nicht benachbart zu einem Objekt verwenden, den Index zu entnehmen.

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

ich so etwas tun würde:

  1. den Vorkommen Graf.
  2. Multiplizieren Sie den Wert mit den Vorkommen.

Schnipsel

// 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

erklären Sie diesen Mann – Mahi

+0

@Mahi Warum können Sie nicht die Kommentare sehen? Welche verstehst du nicht? –

+0

danke für die Antwort, aber ich bin ein ziemlich junger Entwickler ..Ich verstehe diesen Teil nicht counts [num] = zählt [num]? zählt [num] + 1: 1; –

0

Sie könnten eine Hash-Tabelle verwenden und den Index des Ergebnisses Slot speichern. Dies funktioniert auch für unsortierte Werte.

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

nein, nicht wirklich Gott –

+0

sehe meine aktualisierte Lösung. – Alee

0

Ein anderer Vorschlag einschleifigen Array.prototype.reduce und einen hash table verwenden, die die Indizes der Ergebnismatrix wird erzeugt Store - wird Eingangshandhaben, die zu nicht sortiert ist.

Siehe Demo unter:

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

Verwandte Themen