Search code examples
arraysflashapache-flexactionscript-3element

How can add element to array in AS3?


How can add element to array in ActionScript3

If i have an array:

var myArray:Array;

How can add element to this array "myArray", something like this:

myArray[] = value;

My second question is: How can compare if variable value exist in array element value?

Something like in_array function in php


Solution

  • To answer both your questions here, you can add to an array by direct access or by the push() method, like so:

    myArray[7] = something;
    

    or

    myArray.push(something);
    

    Also as Nox noted, you can use the splice method as well to add in elements. This method is used to delete N amount of elements at a specific index, but you can also simultaneously inject one or more elements at the same index.

    For your second question about how to check values or compare them in an array, here is one method:

    var i:int = 0;
    
    for(i; i < myArray.length; ++i){
        if(myArray[i] == 10){
           trace('found');
        }
    }