javascript 數組的remove方法

來源:互聯網
上載者:User

標籤:splice   cal   第一個   影響   call   沒有   注意   tostring   fine   

javascript原生一直沒有提供刪除功能,於是自己寫了幾個remove方法,主要是要注意遍曆數組時使用splice方法是會在遍曆沒完成時就會改變數組對象的length長度,最簡單方法是從數組尾部開始遍曆,用遞減來迴圈,就像我這裡LastRmove的注釋部分,這種方法直觀又不受長度動態變化 的影響,然後我想是不是也要像undescore一樣加上前後順序和匹配到第一個就返回這種功能,於是就作了些小改動,可以從頭部或是尾部匹配第一個移除後就返回,或是遍曆整個數組。
// 遍曆整個數組,移除匹配item的元素,使用強比較===,給第二個參數的話就從頭開始找到第一個匹配item元素移除後返回;// 如有找到元素返回處理後的數組自身,如果沒有找到過就返回undefined;Array.prototype.Remove = function (item, all) {    var result, isType = Object.prototype.toString, i, len, start, hasLast = arguments[2];    start = 0, len = this.length;    for (i = start; i < len;) {        var isPass = true, inx;        if (!hasLast) {            inx = i;        } else {            inx = len - 1;        }        if (isType.call(item) == ‘[object Array]‘) {            for (var ii = 0, iimax = item.length; ii < iimax; ii++) {                if (this[inx] === item[ii]) {                    isPass = false;                    break;                }            }        } else if (this[inx] === item) {            isPass = false;        }        if (!isPass) {            result = true;            this.splice(inx, 1);            if (all) {                break;            }        } else if (!hasLast) {            len = this.length;            i++;        } else {            len--;        }    }    return result ? this : void 0;}// 同上面的Rmove,從尾部開始尋找,找到後刪除第一個匹配的立刻返回;// 如有找到元素返回處理後的數組自身,如果沒有找到過就返回undefined;Array.prototype.LastRemove = function (item) {    /* var result = [], isType = Object.prototype.toString.call,isFrist;     for (var i = this.length - 1; i >= 0; i--) {     var isPass = true;     if (Object.prototype.toString.call(item) == ‘[object Array]‘) {     for (var ii = 0, iimax = item.length; ii < iimax; ii++) {     if (this[i] === item[ii]) {     isPass = false;     break;     }     }     } else if (this[i] === item) {     isPass = false;     }     if (!isPass) {     if(isFrist && !all){     break ;     }     isFrist = true;     this.splice(i, 1);     }     }*/    return this.Remove(item, true, true);}// 效果同上面的,遍曆整個數組,區別是於 返回的是個新的數組,是原數組的引用;Array.prototype.RemoveAt = function (item) {        var result = [], isType = Object.prototype.toString, isPass, val;        for (var inx = 0, len = this.length; inx < len; inx++) {            isPass = true;            val = this[inx];            if (isType.call(item) == ‘[object Array]‘) {                for (var ii = 0, iimax = item.length; ii < iimax; ii++) {                    if (val === item[ii]) {                        isPass = false;                        break;                    }                }            } else if (val === item) {                isPass = false;            }            if (isPass) {                result.push(val);            }        }        return result;    }

 

javascript 數組的remove方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.