All modern browsers except IE have a useful method to find an item in an array by its value- You can add it to IE without changing the others,
it will come in handy again and again.
Code:
Array.prototype.indexOf=
Array.prototype.indexOf || function(what, index){
index= index || 0;
var L= this.length;
while(index< L){
if(this[index]=== what) return index;
++index;
}
return -1;
}
This method removes each of its arguments, if present, from the original array
Code:
Array.prototype.remove= function(){
var what, L= arguments.length, ax;
while(L && this.length){
what= arguments[--L];
while((ax= this.indexOf(what))!= -1){
this.splice(ax, 1);
}
}
return this;
}
to remove all of the elements of one array from another, apply the method:
Code:
var a1=[1,2,3,4,5,6], a2=[5,6,7,8,9];
a1.remove.apply(a1,a2);
returned value: (Array) [1,2,3,4];
Bookmarks