2016-04-29 16 views
2

Ich versuche, einige Informationen in Arrays dynamisch abzurufen, zu berechnen und zu speichern.Gruppiere und summiere ein Array

Informationen, die ich habe, ist:

var myArrayID = []; 
    myArrayID = ['1', '2', '3', '4', '5', '1', '2', '3', '4', '5', '1', ........ '5']; 
    //100 ID in myArrayID that has been sorted in order 

var myArrayScore = []; 
    myArrayScore = ['100', '70', '80', '88', '23', '99', ....... '20']; 
    //100 Score in myArrayScore that has been sorted in order 

Kurz

myArrayID[0] hat Verweis auf myArrayScore[0] ----> ID 1 hat einen Score von 100

....

myArrayID[99] hat eine Referenz auf myArrayScore[99] ----> ID 5 hat eine Punktzahl von 20


Ich mag würde die Gesamtpunktzahl jeder einzelnen ID erhalten, ohne meine hard Methode

Meine Methode ist

var ID1 = 1; 
var ID2 = 2; 
var ID3 = 3; 
var ID4 = 4; 
var ID5 = 5; 

var ID1Score = 0; 
var ID2Score = 0; 
var ID3Score = 0; 
var ID4Score = 0; 
var ID5Score = 0; 

for(var i in myArrayID) 
{ 
    if(myArrayID[i] === ID1) 
    { 
     ID1Score += myArrayScore[i]; 
    } 
    if(myArrayID[i] === ID2) 
    { 
     ID1Score += myArrayScore[i]; 
    } 
    if(myArrayID[i] === ID3) 
    { 
     ID1Score += myArrayScore[i]; 
    } 
    if(myArrayID[i] === ID4) 
    { 
     ID1Score += myArrayScore[i]; 
    } 
    if(myArrayID[i] === ID5) 
    { 
     ID1Score += myArrayScore[i]; 
    }  
} 

Diese Methode funktioniert, aber es ist hart codiert.

Darf ich wissen, ob es sowieso ist es dynamisch zu kodieren,

In einem Sinne, dass, selbst wenn mein Array ändert (vorausgesetzt, ich habe 9-ID statt), aber es ist nach wie vor in einer sortierten Reihenfolge, und es wird nach wie vor enthält die gleiche LÄNGE.

Antwort

1

var myArrayID = ['1', '2', '3', '4', '5', '1', '2', '3', '4', '5']; 
 
var myArrayScore = ['100', '70', '80', '88', '23', '100', '70', '80', '88', '23']; 
 

 

 
var _f = {}; 
 

 
myArrayID.forEach(function(el, i){ 
 
    
 
    if(_f.hasOwnProperty(el)){ 
 
    _f[el] += +myArrayScore[i]; 
 
    } 
 
    
 
    else{ 
 
    _f[el] = +myArrayScore[i]; 
 
    } 
 
    
 
}); 
 

 
alert(JSON.stringify(_f)); 
 

 
// Score for first: 
 
alert("1st Score: "+_f["1"]); 
 

 
// Score for second: 
 
alert("2nd Score: "+_f["2"]);

0

Probieren Sie etwas wie dieses

var outputMap = {}; 
myArray.forEach(function(val,index){ 
    outputMap[val] = outputMap[val] || 0; 
    outputMap[val] += myArrayScore[index]; 
}); 

Jetzt outputMap einen Wert von Gesamtscore gegen jede einzelne ID.

DEMO

var myArrayID = ['1', '2', '3', '4', '5', '1', '2', '3', '4', '5']; 
 
var myArrayScore = ['100', '70', '80', '88', '23', '100', '70', '80', '88', '23']; 
 

 
var outputMap = {}; 
 
myArrayID.forEach(function(val,index){ 
 
    outputMap[val] = outputMap[val] || 0; 
 
    outputMap[val] += parseInt(myArrayScore[index],10); 
 
}); 
 

 
document.body.innerHTML += JSON.stringify(outputMap,0,4);

0

Sie ein Objekt für die Gruppierung der Ergebnisse und Array#forEach für das Iterieren verwenden können.

Zumindest brauchen Sie eine Umwandlung in Nummer.

var myArrayID = ['1', '2', '3', '4', '5', '1', '2', '3', '4', '5', '1', '5'], 
 
    myArrayScore = ['100', '70', '80', '88', '23', '99', '20', '8', '9', '10', '11', '12'], 
 
    score = {}; 
 

 
myArrayID.forEach(function (index) { 
 
    score[index] = (score[index] || 0) + +myArrayScore[index] || 0; 
 
}); 
 

 
document.write('<pre>' + JSON.stringify(score, 0, 4) + '</pre>');

Verwandte Themen