Computersight > Programming > JavaScript

Six Functions Missing From Javascript Arrays

(contd.)

Page 2 of 2 | «Prev12 Next

            right = mid - 1;

        else if (cmp > 0)

            left = mid + 1;

        else

            return mid;

    }

    return -1;

}



/** Returns true if this array contains

 * key; otherwise returns false. */

Array.prototype.contains = function(key, compare) {

    return this.linearSearch(key, compare) != -1;

}



/** Adds all the elements in the

 * specified arrays to this array. */

Array.prototype.addAll = function() {

    for (var a = 0; a < arguments.length; a++) {

        arr = arguments[a];

        for (var i = 0; i < arr.length; i++) {

            this.push(arr[i]);

        }

    }

}



/** Retains in this array all the elements

 * that are also found in the specified array. */

Array.prototype.retainAll = function(arr, compare) {

    if (typeof(compare) == "undefined") {

        compare = simpleCompare;

    }

    for (var i = 0; i < this.length; i++) {

        if (arr.contains(this[i], compare) == false) {

            var end = i + 1;

            while (end < this.length &&

                    arr.contains(this[end], compare) == false) {

                end++;

            }

            this.splice(i, end - i);

        }

        else {

            i++;

        }

    }

}



/** Removes from this array all the elements

 * that are also found in the specified array. */

Array.prototype.removeAll = function(arr, compare) {

    if (typeof(compare) == "undefined") {

        compare = simpleCompare;

    }

    var i = 0;

    while (i < this.length) {

        if (arr.contains(this[i], compare)) {

            var end = i + 1;

            while (end < this.length &&

                    arr.contains(this[end], compare)) {

                end++;

            }

            this.splice(i, end - i);

        }

        else {

            i++;

        }

    }

}



/** Compares two objects using

 * built-in JavaScript operators. */

function simpleCompare(a, b) {

    if (a < b)

        return -1;

    else if (a > b)

        return 1;

    return 0;

}

Using the Six Missing Functions

To use the six functions, include the “array.js” file that you made in the head of an HTML file like this:

<script type="text/JavaScript" src="array.js"></script>

Then you can write JavaScript to use the functions similar to this:

<script type="text/JavaScript">

function test() {

    document.open();


    var setX = [ "apple", "pear", "plum", "peach" ];

    document.writeln('setX: " + setX + "<br />');

    var possible = "plum";

    document.writeln('setX.contains(' + possible + ") " +

            setX.contains(possible) + "<br />");

    possible = "cherry";

    document.writeln('setX.contains(' + possible + ") " +

            setX.contains(possible) + "<br />");


    var setW = [ "cherry", "banana", "apricot", "mango" ];

    setX.addAll(setW);

    setX.sort();

    document.writeln('<br />');

    document.writeln('setX: " + setX + "<br />');

    possible = "peach";

    if (setX.binarySearch(possible) != -1)

        document.writeln('setX contains " + possible + "<br />');

    else

        document.writeln('setX does not contain " + possible + "<br />');

    possible = "coconut";

    if (setX.binarySearch(possible) != -1)

        document.writeln('setX contains " + possible + "<br />');

    else

        document.writeln('setX does not contain " + possible + "<br />');

            

    var setY = [ "elm", "pine", "rose", "lilac" ];

    var setZ = [ "lilac", "pine", "fir" ];

    document.writeln('<br />');

    document.writeln('setY: " + setY + "<br/>');

    document.writeln('setZ: " + setZ + "<br/>');

    setX.addAll(setY, setZ);

    document.writeln('setX.addAll(setY, setZ): " + setX + "<br />');

    setY = [ "elm", "pine", "rose", "lilac" ];

    setY.retainAll(setZ);

    document.writeln('setY.retainAll(setZ): " + setY + "<br />');

    setY = [ "elm", "pine", "rose", "lilac" ];

    setY.removeAll(setZ);

    document.writeln('setY.removeAll(setZ): " + setY + "<br />');


    document.close();

}

</script>

Page 2 of 2 | «Prev12 Next
0
Liked It
I Like It!
Related Articles
The Important Role of Javascript in a Website  |  Object Oriented Programming Strategy (OOPS)
Latest Articles in JavaScript
The Important Role of Javascript in a Website  |  Five Essential Resources for Learning Jquery
Comments (0)
Post Your Comment:
Name:  
Copy the code into this box:  
Post comment with your Triond credentials?
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Powered by
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.