Webmonkey
Powered by Atomz.com



Quick Reference:

• Javascript Code Library
HTML Cheatsheet
Special Characters
Color Codes
Browser Chart
Stylesheet Guide
Unix Guide
Glossary
Domain Registries

Reference   JavaScript Code Library

JavaScript 1.2, which only works in Netscape 4.0, adds several fundamental methods to the array object. A few of these very useful methods are included here in a form that can be used by 3.0 browsers.

The methods added here are:

  • copy - Copies the contents of one array into another.
  • push - Adds an element to the end of an array.
  • pop - Removes an element from the end of an array and shortens the array.
  • shift - Removes an element from the beginning of an array and shortens the array.
  • unshift - Adds an element to the beginning of an array.
  • concat - Tacks the elements of an array onto the end of another array.
  • permute - Randomizes the order of elements in an array.

Usage: Each of the following usage examples assumes you're starting with an array called primates that begins with the elements human, chimp, ape. In other words:

var primates = new Array('human','chimp','ape');

copy
var primates = new Array('human','chimp','ape');
var copy_of_primates = primates.copy();

copy_of_primates will be an array containing the same elements as the primates array.
Be careful if the array contains references to objects instead of just strings or numbers. References to objects will be copied as references. This means that if you change an object in the original array, it will also be changed in the new array.

push
var primates = new Array('human','chimp','ape');
primates.push('lemur');

Now primates contains ('human','chimp','ape','lemur').

pop
var primates = new Array('human','chimp','ape');
var last_primate = primates.pop();

Now primates contains ('human','chimp') and last_primate is 'ape'.

shift
var primates = new Array('human','chimp','ape');
var first_primate = primates.shift();

Now primates contains ('chimp','ape') and first_primate is 'human'.

unshift
var primates = new Array('human','chimp','ape');
primates.unshift('gorilla');

Now primates contains ('gorilla','human','chimp','ape').

concat
var primates = new Array('human','chimp','ape');
var dogs = new Array('puli','pointer','collie');
var mammals = primates.concat(dogs);

Now mammals contains ('human','chimp','ape','puli','pointer','collie').
Concat uses copy, so be careful if the array contains references to objects instead of just strings or numbers. References to objects will be copied as references. This means that if you change an object in the original array, it will also be changed in the new array.

permute
var primates = new Array('human','chimp','ape');
var scrambled_primates = primates.permute();

Now scrambled_primates contains ('ape','human','chimp') or some other random ordering of its elements.
Permute uses copy, so be careful if the array contains references to objects instead of just strings or numbers. References to objects will be copied as references. This means that if you change an object in the original array, it will also be changed in the new array.

License: Webmonkey Public License

Cut, paste, and enjoy!


Feedback  |  Help  |  About Us  |  Contribute  |  Jobs
Advertise  |  Privacy Statement  |  Terms and Conditions

Copyright © 1994-2000 Wired Digital Inc., a Lycos Network site. All rights reserved.