To add a new element to an existing array at a specific index: assign a value to that element. : Array Index « Array « Flash / Flex / ActionScript






To add a new element to an existing array at a specific index: assign a value to that element.

 


package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var fruits:Array = ["apples", "oranges", "pears"];
        
        fruits[3] = "tangerines";
        
        fruits[39] = "grapes";
        
        trace(fruits); // Displays: undefined
        trace(fruits[12]); // Displays: undefined


    }
  }
}
apples,oranges,pears,tangerines,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,grapes
undefined

        








Related examples in the same category

1.Referencing Array Elements
2.Read the data from an array using array-access notation to access one element at a time.
3.Adding Values to an Array: Assigning Values Using Array-Access Notation
4.if you assign a value to an element that has not yet been created, actionscript creates that element, and any other elements with indices between.
5.Use variables or complex expressions to specify an element index
6.Sum the array elements
7.Setting an Element's Value
8.Use variable as the array index
9.Adding New Elements with the length Variable