PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > JavaScript教程 > 正文

    js删除数组元素

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:/**方法:Array.removeAt(Index)*功能:删除数组元素.*参数:Index删除元素的下标.*返回:在原数组上修改数组*/Array.prototype.removeAt=funct...

    1. /* 
    2.  * 方法:Array.removeAt(Index) 
    3.  * 功能:删除数组元素. 
    4.  * 参数:Index删除元素的下标. 
    5.  * 返回:在原数组上修改数组 
    6.  */ 
    7.  
    8. Array.prototype.removeAt = function(Index) { 
    9.         if (isNaN(Index) || Index > this.length) { 
    10.             return false
    11.         } 
    12.         for (var i = 0, n = 0; i < this.length; i++) { 
    13.             if (this[i] != this[Index]) { 
    14.                 this[n++] = this[i] 
    15.             } 
    16.         } 
    17.         this.length -= 1 
    18.     } 
    1. /* 
    2.  * 方法:Array.remove(obj) 
    3.  * 功能:删除数组元素. 
    4.  * 参数:要删除的对象. 
    5.  * 返回:在原数组上修改数组 
    6.  */ 
    7.  
    8. Array.prototype.remove = function(obj) { 
    9.         if (null == obj) { 
    10.             return
    11.         } 
    12.         for (var i = 0, n = 0; i < this.length; i++) { 
    13.             if (this[i] != obj) { 
    14.                 this[n++] = this[i]; 
    15.             } 
    16.         } 
    17.         this.length -= 1 
    18.     } 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-69-3107-1.html
    相关热词搜索: