Actionscript 3:从数组动态构建arrayobjects

时间:2021-04-03 19:33:23

What I want my function todo is to return an dynamically generated array that I then can assign to the value of an object parameter, e.g:

我希望我的函数todo是返回一个动态生成的数组,然后我可以将其分配给一个对象参数的值,例如:

var varObj:Object = {
    level:level,
    scores:getScoreArray()
}

function getScoreArray():Object {
    return[
        for each(var i:score in myScoreArray){
        //append i to the return-array
        }
    ];
}

The resulting varObj should then look something like:

结果varObj应该看起来像:

{level:12,scores:[150,240,550]}

thanks in advance Jery

先谢谢杰瑞

EDIT: So this is what vesper´s answer got me:

编辑:所以这是vesper的回答让我:

private function getTrialsArray():Array {
    var array:Array = new Array();

    for each(var model:TrialTrackingModel in trialTrackingArray) {
        array.push({
            level:model.level,
            stimulustime:model.stimmulusTime,
            inputMethod:model.inputMethod,
            reactionTimes:model.reactionTimes.slice(),
            answers:model.answers.slice()
        });
    }

    return array;
}

1 个解决方案

#1


1  

You basically create a new array in that function, populate it, then return the readied array. Like this:

您基本上在该函数中创建一个新数组,填充它,然后返回准备好的数组。喜欢这个:

function getScoreArray():Array {
    var a:Array=[];
    for each(var i in myScoreArray){
    a.push[i];
    }
    return a;
}

In fact, this can be done with more ease using Array:slice() function.

实际上,使用Array:slice()函数可以更轻松地完成此操作。

function getScoreArray():Array { return myScoreArray.slice(); }

#1


1  

You basically create a new array in that function, populate it, then return the readied array. Like this:

您基本上在该函数中创建一个新数组,填充它,然后返回准备好的数组。喜欢这个:

function getScoreArray():Array {
    var a:Array=[];
    for each(var i in myScoreArray){
    a.push[i];
    }
    return a;
}

In fact, this can be done with more ease using Array:slice() function.

实际上,使用Array:slice()函数可以更轻松地完成此操作。

function getScoreArray():Array { return myScoreArray.slice(); }