2017-04-25 39 views
1

陣列Odds`值I得到從URL JSON數組這樣乘法出`從內JSON

{"soccerodds2017":[ 
    {"Selections":"Chelsea","Odds":"1.44"}, 
    {"Selections":"Wolverhampton","Odds":"2.33"}, 
    {"Selections":"Walsall","Odds":"2.70"} 
]} 

我會喜歡由10 例如乘以賠率:10*1.44*2.33*2.70和得到總。如何在JavaScript中做到這一點?

+1

你嘗試過這麼遠嗎? – gyre

回答

6

您可以使用reduce()只需將累加器的初始值設置爲10,然後再乘以每個e.Odds的值。

var obj = {"soccerodds2017":[{"Selections":"Chelsea","Odds":"1.44"},{"Selections":"Wolverhampton","Odds":"2.33"},{"Selections":"Walsall","Odds":"2.70"}]} 
 

 
var total = obj.soccerodds2017.reduce(function(r, e) { 
 
    return r * +e.Odds 
 
}, 10) 
 

 
console.log(total)

+0

不錯的答案!我喜歡! – funcoding

+0

@funcoding謝謝。 –

+0

將答案減少到2位小數? – meandme

0

你可以讓一個通用的函數來處理處理乘法。

var data = { "soccerodds2017": [ 
 
    { "Selections" : "Chelsea",  "Odds": "1.44" }, 
 
    { "Selections" : "Wolverhampton", "Odds": "2.33" }, 
 
    { "Selections" : "Walsall",  "Odds": "2.70" } 
 
]}; 
 

 
console.log(calculate(data['soccerodds2017'], 'Odds', (x, y) => x * parseFloat(y), 10)); 
 

 
function calculate(data, prop, fn, initVal) { 
 
    return data.reduce((total, value) => fn.call(this, total, value[prop]), initVal || 0); 
 
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

你也可以做一個計算器類來處理所有的樣板邏輯爲您服務。

class Calculator { 
 
    constructor(type = 'float') { 
 
    this.type = type; 
 
    } 
 
    calc(data, prop, op, initVal) { 
 
    initVal = initVal || 0; 
 
    switch (op) { 
 
     case 'add' : return this.__calculate__(data, prop, this.constructor.add, initVal); 
 
     case 'sub' : return this.__calculate__(data, prop, this.constructor.sub, initVal); 
 
     case 'mul' : return this.__calculate__(data, prop, this.constructor.mul, initVal); 
 
     case 'div' : return this.__calculate__(data, prop, this.constructor.div, initVal); 
 
     throw Error('Operation not supported'); 
 
    } 
 
    } 
 
    __calculate__(data, prop, fn, initVal) { 
 
    return data.reduce((total, value) => fn.call(this, total, prop ? value[prop] : value), initVal); 
 
    } 
 
} 
 

 
class IntegerCalculator extends Calculator { 
 
    constructor() { 
 
    super('int') 
 
    } 
 
    static add(x, y) { return x + parseInt(y, 10); } 
 
    static sub(x, y) { return x - parseInt(y, 10); } 
 
    static mul(x, y) { return x * parseInt(y, 10); } 
 
    static div(x, y) { return x/parseInt(y, 10); } 
 
} 
 

 
class FloatCalculator extends Calculator { 
 
    constructor() { 
 
    super('float') 
 
    } 
 
    static add(x, y) { return x + parseFloat(y); } 
 
    static sub(x, y) { return x - parseFloat(y); } 
 
    static mul(x, y) { return x * parseFloat(y); } 
 
    static div(x, y) { return x/parseFloat(y); } 
 
} 
 

 
var floatCalc = new FloatCalculator(); 
 
var data = { "soccerodds2017": [ 
 
    { "Selections" : "Chelsea",  "Odds": "1.44" }, 
 
    { "Selections" : "Wolverhampton", "Odds": "2.33" }, 
 
    { "Selections" : "Walsall",  "Odds": "2.70" } 
 
]}; 
 

 
console.log(floatCalc.calc(data['soccerodds2017'], 'Odds', 'mul', 10));
.as-console-wrapper { top: 0; max-height: 100% !important; }