(e-mail address removed) wrote:
[snip]
array[index]=""
This of course assigns null to the element
It does not. It assigns an empty string to the element. This neither
the same thing, nor does it delete the array element.
To actually delete an array element, you must shift all elements above
the deletion point to overwrite the to-be-deleted elements and then
truncate the array. The splice method does this:
Array.prototype.splice(index, count, item1, item2, ...);
index - The position in the array where deletion/insertion begins.
count - The number of elements to delete.
item1 - An optional argument which specifies a value to insert once
count elements have been deleted.
For example, consider the following actions with array [1, 2, 3, 4]:
result = array.splice(2, 1);
array: [1, 2, 4]
result: [3]
result = array.splice(0, 2);
array: [3, 4]
result: [1, 2]
result = array.splice(3, 1, 5, 6, 7);
array: [1, 2, 3, 5, 6, 7]
result: [4]
result = array.splice(0, 4, 5);
array: [5]
result: [1, 2, 3, 4]
From the penultimate examples, you can see that the splice method can
expand an array if the number of inserted elements outnumbers the
number of deleted elements.
The only problem with the splice method is that IE provided a late
implementation of it, meaning versions earlier than IE5.5 will not
have the method (unless a newer JScript version has been installed).
A complete implementation of splice is included below which can
supplement an older browser:
if('function' != typeof Array.prototype.splice) {
Array.prototype.splice = function(s, dC) {s = +s || 0;
var a = [],
n = this.length,
nI = Math.min(arguments.length - 2, 0),
i, j;
s = (0 > s) ? Math.max(s + n, 0) : Math.min(s, n);
dC = Math.min(Math.max(+dC || 0, 0), n - s);
for(i = 0; i < dC; ++i) {a
= this[s + i];}
if(nI < dC) {
for(i = s, j = n - dC; i < j; ++i) {
this[i + nI] = this[i + dC];
}
} else if(nI > dC) {
for(i = n - 1, j = s + dC; i >= j; --i) {
this[i + nI - dC] = this;
}
}
for(i = s, j = 2; j < nI; ++i, ++j) {this = arguments[j];}
this.length = n - dC + nI;
return a;
};
}
You can just include the code above: if a browser doesn't implement
splice, this code will automatically be used. If a browser does
implement splice, the existing code will be used instead.
If you're only interested in deleting elements from an array, the code
above can be simplified slightly (but maintaining the same semantics) to:
if('function' != typeof Array.prototype.splice) {
Array.prototype.splice = function(s, dC) {s = +s || 0;
var a = [],
n = this.length,
i, j;
s = (0 > s) ? Math.max(s + n, 0) : Math.min(s, n);
dC = Math.min(Math.max(+dC || 0, 0), n - s);
for(i = 0; i < dC; ++i) {
a = this[s + i];
this[s + i] = this[s + i + dC];
}
this.length = n - dC;
return a;
};
}
Finally, a /really/ bare-bones deletion routine would be:
if('function' != typeof Array.prototype.splice) {
Array.prototype.splice = function(s, dC) {
for(var i = 0, n = this.length; i < dC; ++i) {
this[s + i] = this[s + i + dC];
}
this.length = n - dC;
};
}
[snip]
Hope that helps,
Mike