首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有对象和数组的jQuery计算

问带有对象和数组的jQuery计算
EN

Stack Overflow用户
提问于 2014-06-03 15:57:36
回答 2查看 49关注 0票数 1

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

我有一个包含3个资产的对象,每个对象都有一个带有资产id和资产percentAllocation (权重)的键值对,如下所示:

代码语言:javascript
复制
percentAllocations = {Asset1 : '26.7', Asset2 : '30.9', Asset3 : '42.5'}

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

代码语言:javascript
复制
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]]

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

代码语言:javascript
复制
Asset1weight*Asset1return + Asset2weight*Asset2return + Asset3weight*Asset3return

8种场景中的每一种都是...for。最后,我想要一个8值的数组。

我一直在尝试$.each和for循环的每一个组合,我可以想到,但似乎无法得到它。到目前为止,我得到的是一些好事情,但不完全正确.

代码语言:javascript
复制
//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);
    }

});

如果你能给我任何提示,我将不胜感激!即使有人能解释我为什么做不到.我差不多是用这个撞到墙了。谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-03 16:27:57

看上去,你已经循环了两次你的资产。在伪码中:

代码语言:javascript
复制
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

要解决这个问题,我认为您应该首先重新考虑您的数据存储,然后函数将很容易。我建议如下:

代码语言:javascript
复制
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);

我希望这能帮到你!

票数 0
EN

Stack Overflow用户

发布于 2014-06-03 16:27:57

好吧,你可以这样做:

代码语言:javascript
复制
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在第一个foreach lopp中是什么?

下面是演示JsFiddle,检查控制台日志以进行验证。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24019671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档