我有一个这样的数组:var y = [1,2,3];
我想从数组y中移除2。
如何使用jQuery从数组中删除一个特定的值?我尝试过pop(),但它总是删除最后一个元素。
我有一个这样的数组:var y = [1,2,3];
我想从数组y中移除2。
如何使用jQuery从数组中删除一个特定的值?我尝试过pop(),但它总是删除最后一个元素。
当前回答
使用JavaScript安全地从数组中删除2:
// Define polyfill for browsers that don't natively support Array.indexOf()
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
if (this===null) {
throw new TypeError('"this" is null or not defined');
}
var O = Object(this),
len = O.length >>> 0;
if (len===0) return -1;
var n = +fromIndex || 0;
if (Math.abs(n)===Infinity) n = 0;
if (n >= len) return -1;
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (k in O && O[k]===searchElement) return k;
++k;
}
return -1;
};
}
// Remove first instance of 2 from array
if (y.indexOf(2) > -1) {
y.splice(y.indexOf(2), 1);
}
/* To remove all instances of 2 from array, change 'if' to 'while':
while (y.indexOf(2) > -1) {
y.splice(y.indexOf(2), 1);
}
*/
console.log(y); // Returns [1, 3]
Polyfill来源:Mozilla
其他回答
第二个获得最多好评的答案是OP想要的预期行为的一行jQuery方法,但他们在代码的末尾出错了,而且它有一个缺陷。如果要删除的项实际上不在数组中,则最后一项将被删除。
一些人已经注意到了这个问题,一些人已经提供了防止这种情况发生的循环方法。我提供了我所能找到的最短、最干净的方法,并在他们的答案下评论了根据这个方法修复代码的方法。
var x = [1, 2, "bye", 3, 4];
var y = [1, 2, 3, 4];
var removeItem = "bye";
// Removing an item that exists in array
x.splice( $.inArray(removeItem,x), $.inArray(removeItem,x) ); // This is the one-liner used
// Removing an item that DOESN'T exist in array
y.splice( $.inArray(removeItem,y), $.inArray(removeItem,y) ); // Same usage, different array
// OUTPUT -- both cases are expected to be [1,2,3,4]
alert(x + '\n' + y);
数组x将很容易地删除元素“bye”,而数组y将保持不变。
使用参数$. inarray (removeItem,array)作为第二个参数实际上最终是要拼接的长度。由于没有找到该项,因此计算结果为array.splice(-1,-1);,这将导致没有任何内容被拼接…所有这些都不需要为此写一个循环。
//This prototype function allows you to remove even array from array
Array.prototype.remove = function(x) {
var i;
for(i in this){
if(this[i].toString() == x.toString()){
this.splice(i,1)
}
}
}
使用实例
var arr = [1,2,[1,1], 'abc'];
arr.remove([1,1]);
console.log(arr) //[1, 2, 'abc']
var arr = [1,2,[1,1], 'abc'];
arr.remove(1);
console.log(arr) //[2, [1,1], 'abc']
var arr = [1,2,[1,1], 'abc'];
arr.remove('abc');
console.log(arr) //[1, 2, [1,1]]
要使用这个原型函数,您需要将其粘贴到代码中。 然后你可以将它应用到任何带有“点符号”的数组:
someArr.remove('elem1')
不是jQuery的方式,但是… 为什么不用更简单的方法呢?从下面的数组中删除'c'
var a = ['a','b','c','d']
a.splice(a.indexOf('c'),1);
>["c"]
a
["a", "b", "d"]
你也可以使用:(给自己的提示:不要修改你不拥有的对象)
Array.prototype.remove = function(v) { this.splice(this.indexOf(v) == -1 ? this.length : this.indexOf(v), 1); }
var a = ['a','b','c'];
a.remove('c'); //value of "a" is now ['a','b']
添加是simplera.push('c')
试试这个,对我有用
_clientsSelected = ["10", "30", "12"];
function (removeItem) {
console.log(removeItem);
_clientsSelected.splice($.inArray(removeItem, _clientsSelected), 1);
console.log(_clientsSelected);
`enter code here`},
一个工作的JSFIDDLE
你可以这样做:
var y = [1, 2, 2, 3, 2]
var removeItem = 2;
y = jQuery.grep(y, function(value) {
return value != removeItem;
});
结果:
[1, 3]
http://snipplr.com/view/14381/remove-item-from-array-with-jquery/