Array Power, Part III - DHTML Lab | 12 | WebReference

Array Power, Part III - DHTML Lab | 12

Logo

Array Power, Part III
the ind argument


Unlike the other methods we have discussed in this series, which took one type of argument, splice() takes three different types of arguments. The JavaScript1.3/ECMAScript splice() method has specific behaviors when one or more are omitted or a wrong type of argument is passed. We will attempt to account for these in our function.

No ind Argument

removedElements = myArray.splice();

If splice() receives no arguments, it returns a value of undefined. We can duplicate this behavior, by inserting a new first statement to our function:

function array_splice(ind,cnt){

    if(arguments.length == 0) return ind;
    removeArray = this.slice(ind,ind+cnt);
    endArray = this.slice(ind+cnt);
    this.length = ind;
    for(var i=2;i<arguments.length;i++){
        this[this.length] = arguments[i];
    }
    for(var i=0;i<endArray.length;i++){
        this[this.length] = endArray[i];
    }
    return removeArray;
}

We check for the existence of a first argument. If the argument does not exist, that is, if the length of the function's arguments array is 0, we return from the function. The return value is the first argument, ind, which does not exist. Since ind is undefined, the return value is undefined.

ind Argument Is Not a Number

removedElements = myArray.splice("cat");

If splice() is incorrectly passed a non-numeric value as a first argument, it defaults the argument to 0. We insert a statement to account for this posibility:

function array_splice(ind,cnt){
    if(arguments.length == 0) return ind;

    if(typeof ind != "number") ind = 0;
    removeArray = this.slice(ind,ind+cnt);
    endArray = this.slice(ind+cnt);
    this.length = ind;
    for(var i=2;i<arguments.length;i++){
        this[this.length] = arguments[i];
    }
    for(var i=0;i<endArray.length;i++){
        this[this.length] = endArray[i];
    }
    return removeArray;
}

We check the first argument's (ind) type. If it is not a number then we redeclare it with a value of 0, before proceeding.

ind Argument Is a Negative Number

removedElements = myArray.splice(-2);

If splice() is passed a negative number as a first argument, it counts back from the end of the array to find the remove/insert position. That is, a value of -1 points to the last array element, a value of -2 to the second-last, and so on. If the value is the more than the negative equivalent of the array's length, that is if the array has three elements and a value of -9 is passed as the first argument of splice(), the position will be the first element of the array.

function array_splice(ind,cnt){
    if(arguments.length == 0) return ind;
    if(typeof ind != "number") ind = 0;

    if(ind < 0) ind = Math.max(0,this.length + ind);
    removeArray = this.slice(ind,ind+cnt);
    endArray = this.slice(ind+cnt);
    this.length = ind;
    for(var i=2;i<arguments.length;i++){
        this[this.length] = arguments[i];
    }
    for(var i=0;i<endArray.length;i++){
        this[this.length] = endArray[i];
    }
    return removeArray;
}

We check for a negative value for (ind). If we find it, we add it to the array's length, to find a position from the end of the array. This new number cannot be less than 0, so using the Math.max() method, we ensure that it can never point to a non-existant element, before the beginning of the array

ind Argument Is Larger Than Array Length

myArray = [0,"cat"];
removedElements = myArray.splice(9);

If splice() is passed a number pointing to an element beyond the array's length, it considers the number to point to the last element plus one. That is, it will not remove any elements, but it will append any elements passed to the end of the array.

function array_splice(ind,cnt){
    if(arguments.length == 0) return ind;
    if(typeof ind != "number") ind = 0;
    if(ind < 0) ind = Math.max(0,this.length + ind);

    if(ind > this.length) {
        if(arguments.length > 2) ind = this.length;
        else return [];
    }

    removeArray = this.slice(ind,ind+cnt);
    endArray = this.slice(ind+cnt);
    this.length = ind;
    for(var i=2;i<arguments.length;i++){
        this[this.length] = arguments[i];
    }
    for(var i=0;i<endArray.length;i++){
        this[this.length] = endArray[i];
    }
    return removeArray;
}

If ind points to an element beyond the array's length, we enter another conditinal. If there are new elements to add, that is, there are function arguments beyond the first two, then we redeclare ind to point to the last element plus one (this.length). If there are no elements to add, we return from the function, with an empty array as a return value. The logic we use is: since we cannot remove any elements and since there are no elements to add, we might as well return now, saving the execution of the rest of the function statements.

That does it for splice() first argument quirks. There are a couple for the second argument as well.


Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: May 16, 2000
Revised: May 16, 2000

URL: http://www.webreference.com/dhtml/column33/8.html