AddThis Social Bookmark Button
Setting the value of the elements of an array
To set an element's value of an array we use arrayName[elementNumber] as the left-hand operand of an assignment expression
var myChest = new Array("cup","ball","hat","gloves","pen");
myChest[5] = "spoon";
// myChest becomes ["cup","ball","hat","gloves","pen","spoon"]
myChest[0] = "apple";
// myChest becomes ["apple","ball","hat","gloves","pen","spoon"]


Array manipulation in actionscript : useful methods
Array.concat()

The concat() method returns a new array created by appending new elements to the end of an existing array.

var myChest1 = new Array("cup","ball","hat");
myChest1 = myChest1.concat("pen");
// Set myChest1 to [ "cup","ball","hat","pen"]

var myChest2 = new Array("spoon","apple","gloves");
myChest1 = myChest1.concat(myChest2);
// Set myChest1 to [ "cup","ball","hat","pen","spoon","apple","gloves"]


Array.join(delimiter)

The join() method returns a string created by combining all the elements of an array. If you don't specify any delimiter it will be a comma by default.

var myChest = new Array("cup","ball","hat");
myChestString = myChest.join("|");
trace(myChestString);
// output the string cup|ball|hat

This method can be useful if you need to convert an array into a string of strings separated by a comma (or any other delimiter), for example to create a CSV(comma separated values) file with PHP.


Array.pop()

The pop() method deletes the last element of an array. reduces the array's length by 1 and returns the value of the deleted element.

var myChest = new Array("cup","ball","hat");
trace("Now deleting '"+myChest.pop()+"' the new array is : "+myChest);



Array.push()

The push() method appends a list of values to the end of an array as new elements. Elements are added in the order provided. It differs from concat() in that push() modifies the original array, whereas concat() creates a new array.

var myChest = new Array("cup","ball","hat");
myChest.push("spoon","chair");
trace(myChest);
// Output cup,ball,hat,spoon,chair


AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.
flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved