0

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:

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:

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

thanks in advance Jery

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

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 Answer 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.

function getScoreArray():Array { return myScoreArray.slice(); }
Sign up to request clarification or add additional context in comments.

4 Comments

haha damn, didnt think of something like this. I´ll accept your answer as soon as SO allows me to do so. Thanks
cant use slice, because I was oversimplifing my use-case, but that would be a neat option, for something as simple as a score array
There's more. Populating the array out of oyur array is a flexible thing, as you can filter out unneeded objects. Using slice() returns a copy of the array without any filtering, but is faster.
I edited my question to let you see what I acutally did. Slice definitly came in handy. thanks alot. still can´t accept your answer for another 8 mins or so

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.