spacer

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

home / programming / javascript / ncz / column4 / 1 current pageTo page 2
[next]

ASP 3.0/.NET Developer
Jupitermedia
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

Mozilla's New Array Methods

By Nicholas C. Zakas.

When the next version of Firefox is released later this year, it will be sporting several updates to its JavaScript engine. Since JavaScript has mostly remained stagnant since the ECMA-262, 3rd edition was released several years ago, the introduction of new features is very exciting. Part of this JavaScript upgrade directly affects the Array object, which gains several new methods.

A Brief History

The Array object has had a rather sordid and controversial past. In Netscape's original JavaScript implementation, there was no Array object, and developers were forced to create their own. When the first edition of ECMA-262 was released, it formally described an Array object with some basic functionality. Both Netscape and Microsoft implemented the specification, but shortly thereafter, the third edition of ECMA-262 was finalized and introduced even more Array object methods. While Netscape rushed to implement these new methods, Microsoft lagged behind. It wasn't until Internet Explorer reached version 5.5 that the rest of the methods were added. Since that time, there have been no changes to the Array object in any browser.

The New Methods

There are seven new Array methods that can be separated into two categories, item location methods and iterative methods. The item location methods are:

  • indexOf() - returns the index of the given item's first occurrence.
  • lastIndexOf() - returns the index of the given item's last occurrence.
The iterative methods are:

  • every() - runs a function on every item in the array and returns true if the function returns true for every item.
  • filter() - runs a function on every item in the array and returns an array of all items for which the function returns true.
  • forEach() - runs a function on every item in the array.
  • map() - runs a function on every item in the array and returns the results in an array.
  • some() - runs a function on every item in the array and returns true if the function returns true for any one item.
It makes sense to discuss these methods within the context of these two categories.

Item Location Methods

The item location methods, indexOf() and lastIndexOf(), should be familiar to Java and .NET programmers. These methods return the index of an item's first and last occurrence an array, respectively. For example:

var aColors = ["red", "blue", "green", "orange", "purple"];
alert(aColors.indexOf("green")); //outputs "2"
alert(aColors.lastIndexOf("green")); //outputs "2"

In this example, the indexOf() method looks for the string "green" in the array aColors. Since it is the third item in the array, the method returns 2. The lastIndexOf() method also looks for "green" and returns the same value because there's only one instance of "green" in the array. If there were two or more, the results would be different:

Here, the lastIndexOf() method returns 5 while indexOf() returns 2. This is because the lastIndexOf() method starts searching from the end of the array while indexOf() starts from the beginning.

If "green" didn't exist in the array, both methods would return -1. Using indexOf() or lastIndexOf() in this way, you can determine if the given item is in an array, such as:

There is also an optional second argument to the indexOf() and lastIndexOf() methods, which is the index at which to begin searching the array. This is useful since an array can have more than one instance of a given item. For example:

The first indexOf() call returns 2 as usual, while the second returns 5 because it is instructed to begin searching at position 3. From that position, the method continues to search forward and the next instance of "green" is in position 5. The same can be done with lastIndexOf():

In this case, lastIndexOf() is called with a second argument, specifying to start searching from position 4 and work back towards the beginning of the array. Then, the next occurrence of "green" is at position 2.

Using these methods, you can easily get all occurrences of a given item in an array by doing this:

This example finds the first occurrence of the number 9 in an array, then continues to look for more by using the second argument of the indexOf() method. Each index of the number 9 is added to the aIndices array so in the end, you have an array of positions.


home / programming / javascript / ncz / column4 / 1 current pageTo page 2
[next]



JupiterOnlineMedia

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

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes

Created: March 27, 2003
Revised: August 12, 2005

URL: http://webreference.com/programming/javascript/ncz/column4/1