2016-10-03 13 views
0

Ich muss einige Objektwerte in einem Array summieren. Einige können int sein, während andere sein kann string dh:Summe der String-Werte auf Array-Objekt

JavaScript:

let array = [ 
{quantity: 1, amount: "24.99"} 
{quantity: 5, amount: "4.99"}, 
] 

Graben um Stack-Überlauf I this method gefunden haben (Im Reagieren mit):

Array.prototype.sum = function (prop) { 
    var total = 0 
    for (var i = 0, _len = this.length; i < _len; i++) { 
     total += this[i][prop] 
    } 
    return total 
}; 

let totalQuantity = array.sum("quantity"); 
console.log(totalQuantity); 

Während das Arbeiten Toll, ich muss das gleiche für die Zeichenfolge amount tun. Da ich amount in Float konvertieren muss, wird das obige nicht funktionieren. Reagieren Sie beschwert sich über Component's children should not be mutated.

Nicht ninja sein JS, dachte ich, das etwas Magie tun würde:

Array.prototype.sum = function (prop) { 
    var newProp = parseFloat(prop); 
    var total = 0 
    for (var i = 0, _len = this.length; i < _len; i++) { 
     total += this[i][newProp] // Surely this is wrong :(
    } 
    return total 
}; 

jede saubere Art und Weise, dies zu erreichen?

Ich brauche dies:

let totalAmount = array.sum("amount"); 

Antwort

3

definieren eine generische sum Funktion, die als

let sum = a => a.reduce((x, y) => x + y); 

als trivial ist, und es in die Liste der Werte gelten aus dem Quellarray aufgenommen:

let array = [ 
 
{quantity: 1, amount: "24.99"}, 
 
{quantity: 5, amount: "4.99"} 
 
]; 
 
    
 
let sum = a => a.reduce((x, y) => x + y); 
 
    
 
let totalAmount = sum(array.map(x => Number(x.amount))); 
 
    
 
console.log(totalAmount.toFixed(2)) 
 
    
 

+0

Guter Fang. Ich habe mich gefragt, ob es einen anderen Weg gibt, anstatt 'Array.prototype' zu ​​verwenden – Sylar

2

Bitte versuchen:

Array.prototype.sum = function (prop) { 
    var total = 0 
    for (var i = 0, _len = this.length; i < _len; i++) { 
     total += parseFloat(this[i][prop]) // Surely this will work :) 
    } 
    return total 
}; 
+0

Ahhhhhhh! Dank dafür. Es ist gut, ein anderes Auge zu haben, um die Dinge zu betrachten. – Sylar

+1

@Sylar werfen Sie einen Blick auf die array.reduce Version;) –

0

const array = [ 
 
{quantity: 1, amount: "24.99"}, 
 
{quantity: 5, amount: "4.99"} 
 
] 
 
    
 
Array.prototype.sum = function(key) { 
 
    return this.reduce(function(total, item) { 
 
    return total + parseFloat(item[key]); 
 
    }, 0); 
 
} 
 

 
// weird javascript math ... 
 
console.log(array.sum("amount")); 
 

 
// workaround 
 
console.log(array.sum("amount").toFixed(2));

Diese Arbeit gut;)

0

I in der Regel die reduce() Methode für Situationen wie diese. Hier ist eine kleine Demo: http://codepen.io/PiotrBerebecki/pen/zKEQgL

let array = [ 
{quantity: 1, amount: "24.99"}, 
{quantity: 5, amount: "4.99"} 
] 

function sumProperty(arr, type) { 
    return arr.reduce((total, obj) => { 
    if (typeof obj[type] === 'string') { 
     return total + Number(obj[type]); 
    } 
    return total + obj[type]; 
    }, 0); 
} 

let totalAmount = (sumProperty(array, 'amount')).toFixed(2); 
console.log( totalAmount ); // 29.98 

let totalQuantity = sumProperty(array, 'quantity'); 
console.log( totalQuantity ); // 6