Array Object
CSGNetwork FREE Source Code, Javascript Source, and Code Snippets!
CSGNetwork Custom Search
     Bookmark and Share
 
CSGNetwork
Snippets
Calculators
Top Free Apps
Home
Back



Right click this window and select "view source" in order to copy the source for this script.

Array Object

There are three ways of defining an array:

var myfriends=new Array() //1) regular array. Pass an optional integer argument to control array's size.
myfriends[0]="John"
myfriends[1]="Bob"
myfriends[2]="Sue"

var myfriends=new Array("John", "Bob", "Sue") //2) condensed array

var myfriends=["John", "Bob", "Sue"] //3) literal array

Properties

Properties Description
length A read/write property indicating the current number of elements within the array. You may set this property to dynamically expand an array's length.
prototype Use this property to attach additional properties and/or methods that get reflected in all instances of the array.

Methods

Note: "[]" surrounding a parameter below means the parameter is optional.

Methods Description
concat(value1, ...) Concatenates all the argument values to the existing array, and returns the new array. Values can be another array. Example(s).
join([separator]) Converts each element within the array to a string, and joins them into one large string. Pass in an optional separator as argument to be used to separate each array element. If none is passed, the default comma (') is used. Example(s).
pop() Deletes the last element within array and returns the deleted element. Original array is modified.
push(value1, ...) Adds the argument values to the end of the array, and modifies the original array with the new additions. Returns the new length of the array.
reverse() Reverses the order of all elements within the array. Original array is modified.
shift() Deletes and returns the first element within the array. Original array is modified to account for the missing element (so 2nd element now becomes the first etc).
slice(start, [end]) Returns a "slice" of the original array based on the start and end arguments. The slice includes the new array referenced by the start index and up to but NOT including the end index itself. If "end" is not specified, the end of the array is assumed.
splice(startIndex, [how_many], [value1, ...]) Deletes how_many array elements starting from startIndex, and replaces them with value1, value2 etc. Returns the elements deleted from array. Example(s).
sort([SortFunction]) Sorts an array alphabetically and ascending. By passing in an optional SortFunction, you may sort numerically and by other criteria as well. Example(s).
toSource() Returns an array literal representing the specified array.
toString() Returns a string representing the array and its elements.
unshift(value1, ...) Adds the argument values to the beginning of the array, pushing existing arrays back. Returns the new length of the array. Original array is modified.
valueOf() Returns the primitive value of the array.

Examples

concat(value1, ...)

var fruits=["Apple", "Oranges"]
var meat=["Pork", "Chicken"]
var result1=fruits.concat(meat) //creates ["Apple", "Oranges", "Pork", "Chicken"]
var result2=result1.concat(1, 2, 3) //creates ["Apple", "Oranges", "Pork", "Chicken", 1, 2, 3]

join([separator])

var fruits=["Apple", "Oranges"]
var result1=fruits.join() //creates the String "Apple,Oranges"
var result2=fruits.join("*") //creates the String "Apple*Oranges"

sort([SortFunction])

var myarray=["Bob","Bully","Amy"]
myarray.sort() //Array now becomes ["Amy", "Bob", "Bully"]
var myarray2=[7, 40]
myarray2.sort() //Array now becomes [40, 7] since alphabetically, 40 proceeds 7.

For more info on sort(), including sorting an array in numerical order, see this tutorial.

splice(startIndex, [how_many], [value1, ...])

Splice() is one of those array functions that can use some explanation. Lets look at its parameters in detail:

1) startIndex- the array element to begin the insertion or deletion of the array.
2) how_many- the number of elements, beginning with the element specified in startIndex, to delete from the array. Optional. Not specifying this parameter causes all elements starting from startIndex to be deleted.
3) value1, value2 etc- Optional values to be inserted into the array, starting at startIndex.

var myarray=[13, 36, 25, 52, 83]
myarray.splice(2, 2) //myarray is now [13, 36 , 83]. The 3rd and 4th element is removed.

var myarray=[13, 36, 25, 52, 83] //reset array for 2nd example
myarray.splice(2, 3, 42, 15) //myarray is now [13, 36 , 42, 15]. The 3rd, 4th and 5th element is removed, replaced with 42 and 15.



Bookmark and Share
Registered® Trademark™ and Copyright© 1973 - CSG, Computer Support Group, Inc. and CSGNetwork.Com All Rights Reserved