Javascript: How to calculate sum of attribute for each object with a same attribute in an array
<p>I have an array of objects like this one:</p>

<p>{'shapeId': 'shapeid1', 'latlong': '45.42342,-65.23424', 'number': 5}</p>

<p>I want the sum of the values of the attribute "number" when the value of the attribute "shapeId" is the same.</p>

<p>Is there a built in method for that? Thank you!</p>

<p>What I tried so far is this but I only have the last shapeId repeated many times in the output array:</p>

<pre><code>function stats(){
    var total = 0;
    allTotal = [];
    for (var i=0; i &lt; createdShapes.length; i++){
        $.each(allMarkers,function() {
        if(this.shapeId == createdShapes[i].id){
            total += parseInt(this.number);
            shape_id = this.shapeId;
            alert('dans boucle ' + shape_id)
        }
        });
        sums.shapeId = shape_id;
        sums.total = total;
    allTotal.push(sums);
    alert('longueur: ' + allTotal.length)   
    }
    for (var j = 0; j &lt; allTotal.length; j++){
        alert(allTotal[j].shapeId + ':' + allTotal[j].total)
    }   
}       
</code></pre>
