Array Properties.


arrays have an index - zero based, tells you where in the array your string (or selection)

 

var picts = new Array();


picts[1] = "diamond.gif";
picts[2] = "heart.gif";
picts[3] = "spade.gif";
picts[4] = "club.gif";

In the above example, we put an instance of those pictures (really small) at the bottom of the page to preload them


This array (below) allows us to "preload" the images used in our array.
Use the "image object" to have the browser preload the images. - there is a better one, later on

billboardArray = new Array( 3 );

billboardArray[0] = new Image( );
billboardArray[0].src = "images/pict2.gif";
billboardArray[1] = new Image( );
billboardArray[1].src = "images/pict3.gif";
billboardArray[2] = new Image( );
billboardArray[2].src = "images/pict4.gif";

 

// Copies the contents of an array into a temp
// array and returns the copy.
//
// It doesn't alter the original array, but be
// careful if the array contains references to
// objects instead of just strings or numbers.
// References to objects will be copied as
// references. This means that if you change
// an object in the original array, it will
// also be changed in the new array.
//
function copy()
{
var loop;
var temp_array = new Array();
for (loop = 0; loop < this.length; loop++)
{
temp_array[loop] = this[loop];
}
return temp_array;
}

Array.prototype.copy = copy;

// pop
//
// Removes the last element of an array and
// returns that element.
//
function pop()
{
var last_item = this[this.length-1];
this.length--;
return last_item;
}

Array.prototype.pop = pop;

// push
//
// Adds an element to the end of an array and
// returns the new length of the array. This
// method changes the length of the array.
//
function push(the_item)
{
this[this.length] = the_item;
return this.length;
}

Array.prototype.push = push;

 

// concat
//
// Joins two arrays and returns a new array.
// It doesn't alter the original arrays, but be
// careful if the arrays contain references to
// objects instead of just strings or numbers.
// References to objects will be copied as
// references. This means that if you change
// an object in the original arrays, it will
// also be changed in the new array.
//
function concat(second_array)
{
var first_array = this.copy();

for (loop = 0; loop<second_array.length; loop++)
{
first_array[first_array.length] = second_array[loop];
}
return first_array;
}

Array.prototype.concat = concat;

// shift
//
// Removes the first element of an array and
// returns that element.
//
function shift()
{
var new_value = this[0];
var orig_length = this.length;
for (loop = 0; loop<this.length-1; loop++)
{
this[loop] = this[loop+1];
}
this.length--;
return new_value;
}

Array.prototype.shift = shift;

// unshift
//
// Adds an element to the beginning of an array and
// returns the new length of the array.
//
function unshift(the_item)
{
for (loop = this.length-1 ; loop >= 0; loop--)
{
this[loop+1] = this[loop];
}
this[0] = the_item;
return this.length;
}

Array.prototype.unshift = unshift;

// permute
//
// Randomly reorders the elements of an
// array and returns the new, reordered array.
// The original array remains unchanged.
//
function permute(the_array)
{

var temp_array = this.copy();
var new_array = new Array();
var random_num = 0;
for (loop = 0; loop < this.length; loop++)
{
random_num = Math.round(Math.random() * (temp_array.length-1));
new_array[loop] = temp_array[random_num];
temp_array[random_num] = temp_array[temp_array.length-1];
temp_array.length--;
}
return new_array;
}

Array.prototype.permute = permute;


// -->

</script>

JavaScript thinks of arrays as objects. That is, it considers them to be
special entities that know certain things (called properties) and can do
certain things (called methods). Any array you create has properties and
methods that you can take advantage of as a JavaScript programmer.

This exercise introduces you to the most important properties and
methods of the array object.

<HEAD>

<TITLE>Using Array Properties and Methods</TITLE>

<SCRIPT LANGUAGE=JavaScript>

<!--Engage Cloaking

function BuildStirFryArray()

//This function creates and fills an array.

{

StirFry = new Array(5)

StirFry[0] = "Chicken"

StirFry[1] = "Bamboo Shoots"

StirFry[2] = "Baby Corn"

StirFry[3] = "Water Chestnuts"

StirFry[4] = "Soy Sauce"

}

function PrintArray(array)

/* This program prints what's in an array. */

var I

for (i = 0; i <=4; i++)

{

document.write("<P>")

document.write(array[i])

}

}

// Disengage cloaking. -->

</SCRIPT>

</HEAD>

<BODY>

<H1>Stir-Fry: Manipulating an Array of Ingredients</H1>

<SCRIPT LANGUAGE=JavaScript>

<!-- Engage cloaking.

/* This program builds, populates and accesses an array. */

document.write("<P><B>Let's create an array of stir fry
ingredients.</B>")

BuildStirFryArray()

document.write("<P>Okay.")

document.write("<B><P>How many ingredients are in stir fry?</B>")

document.write("<P>")

document.write(StirFry.length)

document.write("<B><P>What are the ingredients in stir fry?</B>")

PrintArray(StirFry)

document.write("<B><P>Yes, but what are the ingredients
<I>backwards</I>?</B>")

PrintArray(StirFry.reverse())

document.write("<B><P>Aha. But can you alphabetize the
ingredients?</B>")

PrintArray(StirFry.sort())

document.write("<B><P>Can't you use commas?</B>")

document.write("<P>")

document.write(StirFry.join(", "))

// Disengage cloaking. -->

</SCRIPT>

<H1>JavaScript-A Culinary Whiz!</H1>

</BODY>

</HTML>

This program uses the array.length property to reveal the number of
elements in StirFry, then uses several array methods to manipulate the
elements. The methods used are:

* array.reverse(), which puts the array elements in reverse order.
* array.sort(), which, without an argument in its parentheses, sorts
the array elements in alphabetical or numerical order. If you want
to get fancy, you can put a call to a sorting function you've
written in the parentheses.
* array.join(), which creates a string of all the array elements,
separated from each other by whatever character(s) you put in the
parentheses. The separator is called a delimiter.


prototype (will create your own properties for all arrray objects in a script)
length
concat() 2 arrays into one array
join() - take all elements of an array and convert them into one string, where each element is separated by a coma
pop() - remove and return the last element of an array (changes it's length)
push() - add element to the end of the array (changes it's length)
reverse() - reverses the order of the elements in an array
shift() - returns first element of the array
slice() - extracts a selectable portion of the array, returns a new array
splice() - adds, removes, or adds and removes elements from an array
toString() - turns the array into a string
unshift() - adds one or more elements before the first element of the array, returns the length of the array