Arrays
Simple Array
- you do not have to declare a fixed size for an array when you create it
- if you do specify a size, the values are NULL until they are set
var myArray = new Array(); myArray[0] = "Alice"; myArray[1] = "Fred"; ...
- if you supply more than one argument to Array(), the values are treated as data for the array
var myArray = new Array("Alice", "Fred", "Jean"); // or var myArray = ["Alice", "Fred", "Jean"];
Dynamically populating an array
- use the .length property to determine the number of items, which is the same as the next available index since arrays are 0 based:
myArray[myArray.length] = "Steve";
Multidimensional Arrays
Strings and Arrays
- use join to create a string of all array items:
var arrayAsString = myArray.join(","); // a comma is inserted between each item
- use split to create an array of items from a string:
var restoredArray = myRegularString.split(",");
- split is more powerful, because regular expressions can be used as the separator parameter; the following creates an array of just the integer portions of values, looking for a period followed by two numerals and an optional comma:
var amounts = "30.25,120.00,45.09,200.10"; var amtArray = amounts.split(/\.\d{2},?/);
Sorting an array
Dividing an array
Combining arrays