[thelist] Remove element from an array using javascript

Liorean Liorean at user.bip.net
Mon Dec 2 07:03:01 CST 2002


At 15:32 2002-12-02 +0300, Stevenson Ngila wrote:
>if i have an array which contains [ 14,8,7,5 ], how do i remove 14 from
>the array using javascript

/* Emulate full Array object support in browsers that doesn't provide full
support already.
  * This is not needed if your target audience are likely to be using
"modern" browsers. */

if(!'shift' in []) // alternatively if(typeof
Array.prototype.shift!='undefined') if you want more backwards
compatibility (eg. versions 4 and below)
   Array.prototype.shift=function(){
     var i=0,
       b=this[0];
     for(i;i<this.length-1;i++)
       this[i]=this[i+1];
   this.length--;
   return b
   };

/* Now, the actual action */
var a=[14,8,7,5];
a.shift();




ie5.5+win, op and moz supports Array.shift natively - this method adds
support to ie5mac and ie5win. Use the alternate version for support of
nn2-4, ie3-4 and a few stray browsers.

// Liorean




More information about the thelist mailing list