2017-04-25 41 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; }