please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 8
Arrays
Next

 


8.6 Array Methods

In the previous section we saw that--in Navigator 3.0 and Internet Explorer 3.0--arrays created with the Array() constructor have a length property. In Navigator 3.0, but not in IE 3.0, these arrays also support three methods that can be used to manipulate the array elements. These methods will be implemented in a future version of IE.

The Array.join() method converts all the elements of the array to a string, and concatenates them, separating them with an optionally specified string passed as an argument to the method. If no separator string is specified, then a comma is used. For example, the following lines of code produce the string "1,2,3":

a = new Array(1,2,3);  // Create a new array with these three elements.
s = a.join();          // s == "1,2,3"

And the following lines specify the optional separator to produce a slightly different result:

a = new Array(1,2,3);
s = a.join(", ");   // s == "1, 2, 3". Note the space after the comma.

In some ways, the Array.join() method is the reverse of the String.split() method which creates an array by breaking a string up into pieces.

The Array.reverse() method reverses the order of the elements of an array. It does this "in place"--i.e., it doesn't create a new array with the elements rearranged, but instead rearranges them in the already existing array. For example, the following code, which uses the reverse() and the join() methods, produces the string "3,2,1":

a = new Array(1,2,3);   // a[0] = 1; a[1] = 2; a[2] = 3;
a.reverse();            // now a[0] = 3; a[1] = 2; a[2] = 1;
s = a.join()            // s = "3,2,1"

The final array method is Array.sort(), which sorts the elements of an array. Like the reverse() method, it does this "in place". When sort() is called with no arguments, it sorts the array elements in alphabetical order (temporarily converting them to strings, to perform the comparison, if necessary):

a = new Array("banana", "cherry", "apple");
a.sort();
s = a.join(", ");   // s == "apple, banana, cherry". 

You can also pass an argument to the sort() method if you want to sort the array elements in some other order. To allow this method to be a fully general sorting algorithm, the optional argument should be a function. This function will be passed two arguments that it should compare. If the first argument should appear before the second in the sorted array, then the function should return a number less than zero. If the first argument should appear after the second in the sorted array, then the function should return a number greater than zero. And if the two values are equivalent (their order is irrelevant), then the function should return 0. So, for example, to sort array elements into numerical, rather than alphabetical order, you might do the following:

a = new Array(33, 4, 1111, 222);
a.sort();                        // alphabetical order:  1111, 222, 33, 4
function numberorder(a,b) { 
    return a-b; 
}
a.sort(numberorder);             // numerical order: 4, 33, 222, 1111

You can probably think of other comparison functions that will sort numbers into various esoteric orders: reverse numerical order, odd numbers before even numbers, etc. The possibilities become more interesting, of course, when the elements you are comparing are objects rather than simple types like numbers or strings.


Previous Home Next
Array Length Property Book Index Arrays in Navigator 2.0


HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/ch08_06.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/12/27 14:21,
TOP NEW HELP FIND: 
3.17.175.191:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/techref/language/java/script/definitive/ch08_06.htm"> [Chapter 8] 8.6 Array Methods</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?