Actionscript 3- Vector to Array

There’s currently no way to convert a Vector to an Array (or ArrayCollection), so here’s a simple util that’ll do it. Accepts any type within the Vector, so the parameter is just an Object, then we convert it back to a vector before the Array conversion. Phew.

public static function VectorToArray( v:Object ):Array
{
    var vec : Vector. = Vector.(v);
    var arr : Array = new Array()
    for each( var i : Object in vec ) {
        arr.push(i);
    }
    return arr;
}

Post to Twitter


2 Responses to “Actionscript 3- Vector to Array”

  • Daniel Bunte Says:

    Hi there!
    Just found this post. Am I allowed to clean and speed it up a bit?

    public static function VectorToArray( v:Object ):Array
    {
    var vec : Vector. = v as Vector.,
    arr : Array = [];
    for each( var i : Object in vec )
    {
    arr[arr.length] = i;
    }
    return arr;
    }

    Cheers,
    Daniel

    PostScriptum: Note this: fast instanceiation(I think the word is wrong, but you know what I mean :) ) is like
    new ['here', 'it', 'is'];

  • Daniel Bunte Says:

    well, it ate the angle brackets… < sorry about that

Leave a Reply