Using Array Methods

The Array object in JavaScript provides a number of useful built-in methods in addition to the pop and push methods I’ve already explained in this chapter. These methods make it easy to quickly operate on arrays.

I’m not going to go over all the methods of the Array object here (mostly because they don’t necessarily apply to working with arrays in other languages). If you do need to work extensively with arrays in JavaScript, you can get a book specifically intended to help with JavaScript programming and look them up.

But you should have a general sense of how array methods work, so we’ll quickly look at the reverse, sort, and concat methods. Rough equivalents to these three Array object methods are available in any modern programming language.

To start with, let’s declare and populate a small (four-element) array to use as an example:

 var theArray = new Array("Neo","Morpheus","Trinity","Mr. Smith"); 

To Reverse the Array:

  1. Call the reverse method of the Array object, for example, with this statement:


The array is now reversed as shown in the second line of text in Figure 6-12. Note that the actual elements of the array have been rearranged in reverse order (this is different from returning a copy of the array with its elements reversed).

click to expand
Figure 6-12: Reversing, sorting, and concatenating an array

To Sort the Array:

  1. Call the sort method of the Array object, for example, with this statement:


The array has now been sorted using a default sort method as shown in the third text line in Figure 6-12. Like the reverse method, the sort method changes the order of the elements in place, this time by sorting them alphabetically (converting numbers to strings as needed).

Note that the default alphabetic sort in JavaScript means—among other things—that all uppercase first letters come before any lowercase first letter. So an array consisting of the elements Apple, ant, Nancy would be sorted as Apple, Nancy, ant —which may not be what you’d like. I show you how to create custom sorts later in this chapter in the “ Making Weird Comparisons ” section.

To Concatenate Elements onto an Array:

  1. Call the concat method of the Array object with the elements you want to concatenate onto the array. For example, to concatenate two new elements onto theArray, use the following:

     theArray.concat("Cypher", "Mouse"); 

The fourth line of text in Figure 6-12 shows the Cypher and Mouse elements concatenated to the existing elements of theArray.

If you open the code shown in Listing 6-10 in your Web browser, you’ll see an original array, then the array reversed, sorted, and with the two concatenated elements.

Listing 6.10: Reversing, Sorting, and Concatenating an Array

start example
 <HTML> <HEAD> <TITLE>  Array methods  </TITLE> <HEAD> <BODY> <H1> <SCRIPT>      var theArray = new Array("Neo","Morpheus","Trinity","Mr. Smith");      document.write ("Original array: " + theArray);      document.write ("<br>");      theArray.reverse();      document.write ("Reversed array: " + theArray);      document.write ("<br>");      theArray.sort();      document.write ("Sorted array: " + theArray);      document.write ("<br>");      document.write("Concatenated: " + theArray.concat("Cypher", "Mouse"));      </SCRIPT> </H1> </BODY> </HTML> 
end example

Learn How to Program Using Any Web Browser
Learn How to Program Using Any Web Browser
ISBN: 1590591135
EAN: 2147483647
Year: 2006
Pages: 115
Authors: Harold Davis

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: