Flash / Flex / ActionScript/Array/Dictionary

Материал из Web эксперт
Перейти к: навигация, поиск

Iterate through the key-value pairs of an object with the for..in and for each..in loops.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var notes:Object = {t: 2, g: 14, s: 9};
        for (var name:String in notes)
        {
            trace("Notes on " + name + ": " + notes[name]);
        }
    }
  }
}



Using an Object as a Lookup Key with Dictionaries

 
package{
  import flash.display.Sprite;
  import flash.utils.*
  public class Main extends Sprite{
    public function Main(){
        var notes:Dictionary = new Dictionary();
        var city:String = "New York City";
        var currentConditions:String = "light showers";
        var currentTemp:Number = 56;
        
        notes[city] = "Central Park";
        notes[currentTemp] = "20";
        notes[currentConditions] = "70% chance of precipitation";
        
        trace("Current Weather for", city, notes[city]);
        trace("Current Temperature :", currentTemp, "degrees", notes[currentTemp]);
        trace("Current Conditions :", currentConditions, "(", notes[currentConditions],")");
    }
  }
}



When using delete on an item in an Array, the length property is not updated.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var pastas:Object = {t: 2, g: 14, s: 9};
        trace(pastas.length); // Displays 9
        delete pastas["s"];
        trace(pastas.length); // Displays undefined
    }
  }
}



Working with Associative Arrays

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var lotto:Array = new Array(12, 30, 42, 6, 29, 75);
        lotto.description = "This week"s lotto picks.";
        trace(lotto[3]); // Displays: 6
        trace(lotto["description"]); // Displays: This week"s lotto picks.
    }
  }
}