/* 方法:Array.remove(dx) * 功能:刪除數組元素. * 參數:dx刪除元素的下標. * 返回:在原數組上修改數組 */ //經常用的是通過遍歷,重構數組. 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 } //在數組中獲取指定值的元素索引 Array.prototype.getIndexByValue= function(value) { var index = -1; for (var i = 0; i < this.length; i++) { if (this[i] == value) { index = i; break; } } return index; } //使用舉例 a = ['1','2','3','4','5']; var dx=a.getIndexByValue("2"); a.remove(dx); //刪除下標爲dx的元素