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]

Senior Consultant/Information Security - permanent position (TX)
Next Step Systems
US-TX-Irving

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

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

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