Javascript 从数组中删除索引数组
jQuery的创建者John Resig创建了一种方法,我总是在项目中使用它:Javascript 从数组中删除索引数组,javascript,jquery,Javascript,Jquery,jQuery的创建者John Resig创建了一种方法,我总是在项目中使用它: // 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;
// 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);
如果Array.remove()
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
}
};
这个版本应该有效。它修改原始数组。如果希望在不修改原始数组的情况下返回新数组,请使用注释掉的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]
我想这就是你想要的(它也适用于负指数):
if(!Array.prototype.removeIndexes){
Array.prototype.removeIndexes=函数(索引){
var arr=此;
如果(!jQuery)抛出新引用错误('jQuery未加载');
var偏移=0;
对于(var i=0;i=arr.length)
抛出新错误(“索引超出范围”);
}
index=index.sort();
对于(var i=0;i
也许吧?不要修改你不拥有的对象-即使J.Resig这样做了so@techfoobar谢谢,我不知道那个网站。谢谢。如果我传递了一个不存在的索引,会发生什么?如-1
如果传递了错误的索引,将抛出错误。您可以使用负数从数组末尾进行选择(作为拼接函数)。顺便说一句,在索引[i]=arr.length+index中,索引
未定义代码>。你是说索引[i]=arr.length+索引[i]代码>?@Johan:此函数返回一个新数组,而不修改原始数组。好的,可以调整它以修改您正在使用它的实际数组吗?谢谢。splice()
将如何处理-1
-index?它不会。[a',b',c',d',e',f'];返回[a,b,d,e]而不是[a,b,c,e]
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]
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