jQuery使用对象和数组进行计算

时间:2014-06-03 15:57:37

标签: jquery arrays object math

我正在尝试计算不同经济情景的年度回报。

我有一个包含3个资产的对象,每个资产都是一个具有资产ID和资产百分比(权重)的键值对,如下所示:

percentAllocations = {Asset1 : '26.7', Asset2 : '30.9', Asset3 : '42.5'}

我还有一个包含3个数组返回值的数组。每个子数组都有8种不同场景的返回值,如下所示:

assetReturns = [[13.13, 4, -12.23, -23.55, 4.13, -2.3, 18.9, -12.43], [12.12, 4.07, -15.56, -22.98, 4.2, -2.29, 18.92, -12.43],[ 12.12, 4.11, -15.77, -23, 4.05, -2.31, 18.85, -12.34]]

我需要做的是针对每个场景,将每个资产的权重乘以相同对应资产的回报,并将它们全部加在一起。更清楚:

Asset1weight*Asset1return + Asset2weight*Asset2return + Asset3weight*Asset3return

...对于8个场景中的每一个。最后,我想要一个数组或8个值的东西。

我一直在尝试$ .each和for循环的每一个组合,我能想到并且似乎无法得到它。这是我到目前为止所做的事情,这是做了一些好事,但不是很正确......

//for each asset in the percentAllocations(weights) object
$.each(weights, function(id, weight){  

    //for each scenario        
    for(i=0; i<numberScenarios; i++){//8 scenarios
        //make a placeholder variable
        var temp=0;
        //for each asset
        for(j=0; j<numberFunds; j++){//3 assets

            //multiply the weight of the asset by the return of the asset for that particular scenario
            //add it it to temp
            //is this what is happening? no.
            temp += parseFloat((weight/100)*assetReturns[j][i]);
            console.log("weight of "+id+" x assetReturns["+j+"]["+i+"]= "+temp);

        }
        console.log(temp);
    }

});

您可以给我的任何提示将不胜感激!即使有人能够解释为什么我所做的事情也不起作用......我几乎已经碰到了这个问题。谢谢!

2 个答案:

答案 0 :(得分:0)

你似乎正在两次循环你的资产。在伪代码中:

go through every asset;
for each of those assets:
    go through all possible scenarios;
    for each scenario: 
        go through each asset
        for each asset:
            add value to sum

要解决此问题,我认为您应首先重新考虑数据存储,然后功能将很容易。我建议像:

function Asset(name, allocation, returns) {
    this.name=name;
    this.allocation=allocation;
    this.returns=returns;

    this.value = function(scenario) {
        return this.weight * this.returns[scenario];
    }
}
// same assets you had, structured differently
var assets = [
    new Asset('Asset1', 26.7, [13.13, 4, -12.23, -23.55, 4.13, -2.3, 18.9, -12.43]),
    new Asset('Asset2', 30.9, [12.12, 4.07, -15.56, -22.98, 4.2, -2.29, 18.92, -12.43],
    new Asset('Asset3', 42.5, [ 12.12, 4.11, -15.77, -23, 4.05, -2.31, 18.85, -12.34]
];
var number_of_scenarios = 8; // not ideal, but when you create returns should be set, not here
var total_returns = [];
for(var i = 0; i < number_of_scenarios; i++) {
    var ret = 0;
    $.each(assets, function(id, assset) {
        // this could be done with a normal for loop too
        ret += asset.value(i);
    }
    total_returns.push(ret);
}
console.log(total_returns);

我希望这有帮助!

答案 1 :(得分:0)

好的,你可以这样做:

var percentAllocations = {Asset1 : '26.7', Asset2 : '30.9', Asset3 :'42.5'};
var assetReturns = [[13.13, 4, -12.23, -23.55, 4.13, -2.3, 18.9, -12.43], [12.12, 4.07, -15.56, -22.98, 4.2, -2.29, 18.92, -12.43],[ 12.12, 4.11, -15.77, -23, 4.05, -2.31, 18.85, -12.34]];
alert(JSON.stringify(percentAllocations));  
alert(assetReturns);

//for each asset in the percentAllocations(weights) object
$.each(percentAllocations, function(id, weight){  
    console.log(id);
    console.log(weight);
    //for each scenario  in assetReturns
    $.each(assetReturns,function(i,value){
        //Gives single scenario like [13.13, 4, -12.23, -23.55, 4.13, -2.3, 18.9, -12.43]
        var temp=0;
        console.log(value);
        $.each(value,function(i,singleScenarioValue){
            //single value obtained from assetReturns : single scenario 
            temp += parseFloat((weight/100)*singleScenarioValue);
        });
                console.log(temp);

    });
});

我无法从你的问题中找出问题的原因。但是你应该检查一些事情

  • percentAllocations定义完全错误
   percentAllocations = {Asset1 : '26.7', Asset2 : '30.9', Asset3 : '42.5'}
  • 另一个是,weights lopp
  • 中的foreach是什么

这是Demo JsFiddle检查控制台日志以进行验证。