使用对象和数组的jQuery计算

使用对象和数组的jQuery计算,jquery,arrays,object,math,Jquery,Arrays,Object,Math,我试图计算不同经济情景下的年度回报 我有一个对象,其中有3个资产,每个资产都是一个键值对,带有资产id和资产百分比分配(权重),如下所示: percentAllocations = {Asset1 : '26.7', Asset2 : '30.9', Asset3 : '42.5'} 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, -

我试图计算不同经济情景下的年度回报

我有一个对象,其中有3个资产,每个资产都是一个键值对,带有资产id和资产百分比分配(权重),如下所示:

percentAllocations = {Asset1 : '26.7', Asset2 : '30.9', Asset3 : '42.5'}
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]]
我还有一个包含3个数组返回值的数组。每个子数组都有8种不同场景的返回值,如下所示:

percentAllocations = {Asset1 : '26.7', Asset2 : '30.9', Asset3 : '42.5'}
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个值的东西

我一直在尝试$的每一个组合。每一个和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);
    }

});
//对于percentAllocations(weights)对象中的每个资产
$。每个(权重,函数(id,权重){
//对于每个场景

对于(i=0;i您似乎在两次循环您的资产。在伪代码中:

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);
功能资产(名称、分配、返回){
this.name=name;
分配=分配;
this.returns=returns;
this.value=函数(场景){
返回this.weight*this.returns[scenario];
}
}
//你拥有相同的资产,结构不同
var资产=[
新资产(“资产1',26.7,[13.13,4,-12.23,-23.55,4.13,-2.3,18.9,-12.43]),
新资产(‘资产二’、30.9、[12.12、4.07、-15.56、-22.98、4.2、-2.29、18.92、-12.43],
新资产(‘资产三’、42.5、[12.12、4.11、-15.77、-23、4.05、-2.31、18.85、-12.34]
];
var number_of_scenarios=8;//不理想,但在创建返回时应该设置,而不是在这里
var总收益=[];
对于(var i=0;i<场景的数量;i++){
var-ret=0;
美元。每个(资产、功能(id、assset){
//这也可以通过正常的for循环来实现
ret+=资产价值(i);
}
总返回量。推送(ret);
}
console.log(返回的总次数);

我希望这有帮助!

好的,您可以这样做:

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);

    });
});
我无法从你的问题中找出问题的原因。但有几件事你应该检查一下

  • 百分比分配的定义完全错误,应该是这样的
  • 另一个问题是,第一个
    foreach
    lopp中的
    weights
    是什么

这是演示,请检查控制台日志以进行验证。

您的percentAllocations对象语法错误:您应该使用冒号而不是等号谢谢,修复了。我刚刚从firebug控制台复制了它。