Tuesday, December 11, 2012

Associative array with an indexed array

To make the individual arrays even easier to access, you can use an associative array for the days of the week, and use indexed arrays for the task lists. Using an associative array not only allows you to avoid the use of an enumeration object, but also allows you to use dot syntax when referring to a particular day of the week:
var masterTaskList:Object = new Object();
masterTaskList["Monday"] = ["wash dishes", "take out trash"];
masterTaskList["Tuesday"] = ["wash dishes", "pay bills"];
masterTaskList["Wednesday"] = ["wash dishes", "dentist", "wash dog"];
masterTaskList["Thursday"] = ["wash dishes"];
masterTaskList["Friday"] = ["wash dishes", "clean house"];
masterTaskList["Saturday"] = ["wash dishes", "wash car", "pay rent"];
masterTaskList["Sunday"] = ["mow lawn", "fix chair"];
The use of dot syntax makes the code more readable by allowing you to avoid the use of multiple sets of brackets:
trace(masterTaskList.Wednesday[1]); // Output: dentist
trace(masterTaskList.Sunday[0]); // Output: mow lawn
Programming Actionscript 3. Powered by Blogger.