spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / java_core / 1 To page 1To page 2To page 3current pageTo page 5To page 6To page 7
[previous] [next]

Data Center Architect
The Computer Merchant, Ltd
US-MA-chelsea

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


JavaScript by Example: JavaScript Core Objects. Pt. 1

The push() Method

The push() method adds new elements onto the end of an array, thereby increasing the length of the array. JavaScript allocates new memory as needed.

 

Arrayname.push(new elements); // Appended to the array

 

Example:

 

myArray.push("red", "green", "yellow");

 

<html>

<head><title>Array push() method</title>

</head>

<body>

<script language="JavaScript">

1 var names=new Array("Tom", "Dan", "Liz", "Jody");

2 document.write("<b>Original array: "+ names + "<br>");

3 names.push("Daniel","Christian");

4 document.write("New array: "+ names + "</b>");

</script>

</body>

</html>

 

 
An Array object called names is declared and intialized.
  1. The contents of the array (i.e., all of its elements) are displayed.

  2. The push() method appends two new elements, "Daniel" and "Christian", to the end of the names array

    .
  3. The array has grown. It is displayed in the browser window with its new elements. (See Figure 9.8.)

 
The shift() and unshift() Methods

The shift() method removes the first element of an array and returns the value shifted off; the unshift() method adds elements to the beginning of the array. These methods are just like pop() and push() except that they manipulate the beginning of the array instead of the end of it.

 

var return_value=Arrayname.shift();

Arrayname.shift( new elements); // Prepended to the array

 

Example:

 

var shifted_off = myArray.shift();

myArray.shift("blue","yellow");

 

<html>

<head><title>Array shift() and unshift() methods</title>

</head>

<body>

<script language="JavaScript">

1 var names=new Array("Dan", "Liz", "Jody" );

document.write("<b>Original array: "+ names + "<br>");

2 names.shift();

document.write("New array after the shift: " + names);

3 names.unshift("Nicky","Lucy");
// Add new elements to the beginning of the array

document.write("<br>New array after the unshift: " + names);

</script>

</body>

</html>

 

 
A new Array object called names is created.
  1. The first element of the array is shifted off, shortening the array by 1.

  2. The unshift() method will prepend to the beginning of the array, the names "Nicky" and "Lucy", thereby making it longer by two elements. (See Figure 9.9.)

 

The shift() and unshift() methods.

The slice() Method

The slice() method copies elements of one array into a new array. The slice() method takes two arguments: the first number is the starting element in a range of elements that will be copied, and the second argument is the last element in the range, but this element is not included in what is copied. Remember that the index starts at zero, so that a beginning position of 2 is really element 3. The orginal array is unaffected unless you assign the result of the slice back to the original array.

var newArray = Arrayname.slice(first element, last element);

 

Example:

 

var ArraySlice = myArray.slice(2,6); // ArraySlice contains elements

// 2 through 5 of myArray.

 

 

<html>

<head><title>Array slice() method</title>

</head>

<body>

<script language="JavaScript">

1 var names=new Array("Dan", "Liz", "Jody", "Christian",

"William");

document.write("<b>Original array: "+ names + "<br>");

2 var sliceArray=names.slice(2, 4);

document.write("New array after the slice: ");

3 document.write(sliceArray);

</script>

</body>

</html>

 

This is the original array of names.
  1. The slice() method will start at position 2, copy Jody into the new array, then Christian, and stop just before position 4, William. The original array is not affected by the slice.

  2. The new array created by the slice() method is displayed. (See Figure 9.10.)

Using slice() to create a new array.

The splice() Method

The splice() method [not to be confused with slice()] removes a specified number of elements from some starting position in an array and allows you to replace those items with new ones. (Don't confuse this method with the slice() method. Ropes, tapes, and films are spliced; bread, meat, and golf balls are sliced.)

 

Arrayname.splice( index position, number of elements to remove);

Arrayname.splice(index position, number of elements to remove,

replacement elements);

 

Example:

 

myArray.splice( 3, 2);

myArray.splice( 3, 2, "apples","oranges");

 

 

<html>

<head><title>Array splice() method</title>

</head>

<body>

<script language="JavaScript">

// splice(starting_pos, number_to_delete, new_values)

1 var names=new Array("Tom","Dan", "Liz", "Jody");

document.write("<b>Original array: "+ names + "<br>");

2 names.splice(1, 2, "Peter","Paul","Mary");

3 document.write("New array: "+ names + "</b>");

</script>

</body>

</html>

 

 
An Array object called names is declared and intialized.
  1. The splice() method allows you to delete elements from an array and optionally replace the deleted elements with new values. The first arguments to the splice method are 1, 2. This means: start at element 1, and remove a length of 2 elements. In this example, element 1 starts with "Dan" (element 0 is "Tom"). "Liz" is the second element. Both "Dan" and "Liz" are removed. The next three arguments, "Peter", "Paul", and "Mary", are then inserted into the array, replacing "Dan" and "Liz".

  2. The new names array is displayed in the browser window. (See Figure 9.11.)

 

The splice() method.

home / programming / java_core / 1 To page 1To page 2To page 3current pageTo page 5To page 6To page 7
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: November 19, 2003

URL: http://webreference.com/programming/java_core/1