簡體   English   中英

從數組中刪除索引數組

[英]Remove array of indexes from array

jQuery的創建者John Resig創建了一個非常方便的Array.remove方法,我總是在我的項目中使用它:

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

// Remove the second item from the array
array.remove(1);
// Remove the second-to-last item from the array
array.remove(-2);
// Remove the second and third items from the array
array.remove(1,2);
// Remove the last and second-to-last items from the array
array.remove(-2,-1);

它很棒。 但我想知道它是否可擴展,以便它可以將一系列索引作為第一個參數?

否則,我可能會使用另一種方法來使用它:

if (!Array.prototype.removeIndexes) {

    Array.prototype.removeIndexes = function (indexes) {

        var arr = this;

        if (!jQuery)
            throw new ReferenceError('jQuery not loaded');

        $.each(indexes, function (k, v) {

            var index = $.inArray(v, indexes);

            if (index !== -1)
                arr.remove(index);

        });
    };
}

如果Array.remove()不能擴展以滿足我的需求,您對我上面的其他解決方案有什么看法?

我認為這就是你要找的東西(它也適用於負面指數):

if (!Array.prototype.removeIndexes) {

    Array.prototype.removeIndexes = function (indexes) {

        var arr = this;

        if (!jQuery) throw new ReferenceError('jQuery not loaded');

        var offset = 0;

        for (var i = 0; i < indexes.length - 1; i++) {
            if (indexes[i] < 0)
                indexes[i] = arr.length + indexes[i];
            if (indexes[i] < 0 || indexes[i] >= arr.length)
                throw new Error('Index out of range');
        }

        indexes = indexes.sort();

        for (var i = 0; i < indexes.length - 1; i++) {
            if (indexes[i + 1] == indexes[i])
                throw new Error('Duplicated indexes');
        }


        $.each(indexes, function (k, index) {
            arr.splice(index - offset, 1);
            offset++;
        });
        return arr;
    };
}

var a = ['a', 'b', 'c', 'd', 'e', 'f'];
var ind = [3, 2, 4];

a.removeIndexes(ind);

console.log(a.join(', '));
// returns : a, b, f 

看小提琴

這個版本應該工作。 它修改了原始數組。 如果你喜歡不修改原始返回一個新的數組,使用的注釋掉初始化result ,並添加return result的函數結束。

Array.prototype.removeIndexes = function(indices) { 
  // make sure to remove the largest index first
  indices = indices.sort(function(l, r) { return r - l; });

  // copy the original so it is not changed
  // var result = Array.prototype.slice.call(this);

  // modify the original array
  var result = this;

  $.each(indices, function(k, ix) {
    result.splice(ix, 1);
  });
}

> [0, 1, 2, 3, 4, 5, 6, 7, 8].removeIndexes([4, 5, 1]);
> [0, 2, 3, 6, 7, 8]

怎么樣

Array.prototype.remove = function (indexes) {
    if(indexes.prototype.constructor.name == "Array") {
        // your code to support indexes
    } else {
        // the regular code to remove single or multiple indexes
    }

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM