Contents of this section:
js delete specified elements in Array array
Method 1,
/* * Method: Array.remove(dx) Reconstruct the array through traversal* Function: Delete array elements. * Parameters: dx delete the subscript of the element. */ Array.prototype.remove=function(dx) { if(isNaN(dx)||dx>this.length){return false;} for(var i=0,n=0;i<this.length;i++) { if(this[i]!=this[dx]) { this[n++]=this[i] } } this.length-=1 } a = ['1','2','3','4','5']; alert("elements: "+a+"/nLength: "+a.length); a.remove(1); //Delete the element with subscript 1 alert("elements: "+a+"/nLength: "+a.length);Method 2,
/* * Method: Array.baoremove(dx) * Function: Delete array elements. * Parameters: dx delete the subscript of the element. * Return: Modify the array on the original array. */ Array.prototype.baoremove = function(dx) { if(isNaN(dx)||dx>this.length){return false;} this.splice(dx,1); } b = ['1','2','3','4','5']; alert("elements: "+b+"/nLength: "+b.length); b.baoremove(1); //Delete the element with subscript of 1 alert("elements: "+b+"/nLength: "+b.length);The two methods of deleting specified elements in the Array array in the above article js are all the content shared by the editor. I hope it can give you a reference and I hope you will support Wulin.com more.