| home / programming / java_core / 1 | [previous] [next] |
|
|
Whether you have an array of colors, names, or numbers, there are many ways you might want to manipulate the array elements. For example, you might want to add a new name or color to the beginning or end of the array, remove a number from the end of the array, or sort out all the elements, reverse the array, and so on. JavaScript provides a whole set of methods for doing all of these things and more. See Table 9.2.
|
Joins the elements of an array by a separator to form a string |
|
|
Returns a string representation of the array in local format |
|
The concat() method concatenates the elements passed as arguments onto an existing array (JavaScript 1.2), returning a new concatenated list.
|
newArray=oldArray.concat(new elements); |
|
|
|---|
|
<head><title>Array concat() methods</title> <script language="JavaScript"> 1 var names1=new Array("Dan", "Liz", "Jody" ); 2 var names2=new Array("Tom", "Suzanne"); document.write("<b>First array: "+ names1 + "<br>"); document.write("<b>Second array: "+ names2 + "<br>"); document.write("<b>After the concatenation <br>"); |
|
|
|---|
The first Array object, called names1, is created. |
The pop() method deletes the last element of an array and returns the value popped off.
|
|
|---|
|
|
|---|
|
<head><title>Array pop() method</title> <script language="JavaScript"> 1 var names=new Array("Tom", "Dan", "Liz", "Jody"); 2 document.write("<b>Original array: "+ names +"<br>"); 3 var newstring=names.pop(); // Pop off last element of array 4 document.write("Element popped: "+ newstring); |
|
|
|---|
The Array() constructor creates a new array called names and intializes the array with four values: "Tom", "Dan", "Liz", and "Jody". |
| home / programming / java_core / 1 | [previous] [next] |
| ||||||||||||||||||||
Created: March 27, 2003
Revised: November 19, 2003
URL: http://webreference.com/programming/java_core/1