js删除Array数组中的某个元素

删除数组
方法一
/*
*  方法: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
}
a = ['1','2','3','4','5'];
alert("elements: "+a+"\nLength: "+a.length);
a.remove(1); //删除下标为1的元素
alert("elements: "+a+"\nLength: "+a.length);



</script>

方法二

/*
*  方法:Array.baoremove(dx)
*  功能:删除数组元素.
*  参数:dx删除元素的下标.
*  返回:在原数组上修改数组.
* splice方法见 http://www.w3school.com.cn/js/jsref_slice_array.asp
*/



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); //删除下标为1的元素
alert("elements: "+b+"\nLength: "+b.length);
</script>

你可能感兴趣的:(prototype,asp)