HTML and CSS Reference
In-Depth Information
Using Array Methods
• To reverse the order of items in an array, use the method
array .reverse()
where array is the name of the array.
• To sort an array in alphabetical order, use the following method:
array .sort();
• To sort an array in any order, use
array .sort( function )
where function is the name of a compare function that returns a positive, negative,
or 0 value.
• To extract items from an array without affecting the array contents, use
array .slice( start , stop )
where start is the index of the array item at which the slicing starts and stop is the
index before which the slicing ends. If no stop value is provided, the array is sliced to
the end of the array.
• To remove items from an array, use
array .splice( start , size )
where start is the index of the array item at which the splicing starts and size is the
number of items to remove from the array. If no size value is specified, the array is
spliced to its end.
• To add items to an array, use
array . splice ( start , size , values)
where values is a comma-separated list of new values to replace the old values in
the array.
• To add new items to the end of an array, use
array .push( values )
where values is a comma-separated list of values.
• To remove the last item from an array, use the following method:
array .pop()
Figure 12-11 summarizes several other methods that can be applied to arrays. Arrays
are a powerful and useful feature of the JavaScript language. The methods associated
with arrays can be used to simplify and expand the capabilities of Web page scripts.
Search WWH ::




Custom Search