繁体   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