Array Handling (Javascript):   Return to List

' General Notes: (1) Unlike C/C++, Javascript allows the size of the array to change DYNAMICALLY at any time. (2) Can change the size by simply altering the .length attribute (though if reduced -- data may be lost). (3) Can also mix numbers with strings.

// To create a one dimensional array:
var x = new Array();        // an empty array created with constructor parameters empty
    -- or --
var x = new Array(6);        // an array created to hold 7 items (since we're zero-based)
    -- or --
var x = ["red", "yellow", "green", 1, 5, 8, "horse"];    // using square brackets and filling array in directly

// Also possible to index arrays using strings (as with a Hash table):
products["shirts"] = 46;
var y = products["shirts"];    // var y now equals 46

// Creates and Loads an array (using a separate function):
// (a) Create a product quantity array:
    var productQty = new Array();
// (b) Call the function which will load the array:
    populateArray(productQty);

// (c) Function which does the work of populating array:
function populateArray(products)
{
    products["shirts"]=46;
    products["pants"]=23;
    products["hats"]=14;
}

Methods available to the array object are as follows:
// (a) Create a dbaseField() array:
    var dbaseFields = new Array();

// join() function -- concatenates all elements into one string (see also the .toString() method below)
    strTemp = dbaseFields.join(",");    // can specify delimiters other than comma; no spaces will be inserted

// .length property -- gives the size of the array
    x = dbaseFields.length;

// reverse() function -- reverses the order of the elements in the array
    dbaseFields.reverse();    // reverses the order of elements -- regardless of the sequence

// sort() function -- sorts elements in the array
    dbaseFields.sort();

// concat() function -- concatenates array #2 (shelf_2) onto array #1 (shelf_1)
    inventory = shelf_1.concat(shelf_2);        // a new array (inventory) was created from the other two
                            // resulting array didn't need to be previously defined

// slice() function -- returns a subsection of the array. A new array is created with the specified parts of the source array.

// Syntax:     array.slice(start)        The position in the array where the slice is to begin. Negative
                        numbers can be used to count from the last element to the first.
                        For example: -1 is the last element in the array, and -2 is the
                        second to last element in the array.
        array.slice(start, stop)    The position in the array where the slice is to stop. Like the start
                                parameter, the stop parameter can be negative.

// Example:    myArray = new Array(345, 23, 756, 5);

        mySmallArray = myArray.slice(1, 3); // mySmallArray contains [23, 756] -- stop element is NOT included

// splice() function -- inserts and removes elements from an array

// Syntax:    array.splice(start, delete, arg3, . . . , argN)
                start = Position in the array where the splice is to begin
                delete = The number of elements to be deleted from the array, beginning
                     at the position specified by start.
                arg3,...argN = New array elements to be inserted into the array, starting
                     at the position specified by start.

// Example:    foodOrder = new Array("hamburger", "fries", "drink");
        foodOrder.splice(0, 1, "hotdog");    // hotdog would replace hamburger

// push() function -- adds elements to the end of the array
    dbaseFields.push("Name", "Address");    // adds the new elements "Name" and "Address" to the array

// pop() function -- deletes the last element from an array
    dbaseFields.pop();    // last element was removed (and array resized)

// shift() function -- deletes elements from the front of an array
    dbaseFields.shift(); // first element was removed from the array (and array resized)

// unshift() function -- add elements to the front of the array
    grades = new Array(95, 97);
    newLength = grades.unshift(100, 93);    // newLength equals (100, 93, 95, 97)

// toString() function -- returns one string that contains all the elements in the array separated with commas.
//                Note: This is the method used by Javascript to convert an array to a string automatically
//                    when an array is used in string context (as opposed to join()).

    sTemp = dbaseFields.toString();

// toSource() function -- converts elemetns to a string with square brackets. Just as it would appear as if defining
//                    the array initially.
    sTemp = dbaseFields.toSource();
    myNums = new Array(3, 6, 7);
    myValues = newArray("Blue", "Green", "Red", myNums);
    sTemp = myValues.toSource();    // sTemp equals ["Blue", "Green", "Red" [3, 6, 7]]

// valueOf() function -- performs a similar function as toSource() (above), but leaves off the square brackets and
                flattens any arrays within arrays. In other words, leaves off internal square
//                    brackets -- as would be present in the .toSource() output.
    sTemp = myValues.valueOf();    // sTemp equals "Blue", "Green", "Red", 3, 6, 7