我有一个这样的数组: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
其他回答
不是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')
使用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
我有一个类似的任务,我需要根据数组中对象的属性一次性删除多个对象。
所以经过几次迭代后,我最终得到:
list = $.grep(list, function (o) { return !o.IsDeleted });
Filter方法在vanilla js中很有用。这可用于Array对象。
var arr = [1, 2, 3, 4, 5, 5];
var result = arr.filter(function(elem){
return elem != 5;
});//result -> [1,2,3,4]
http://jsfiddle.net/emrefatih47/ar0dhvhw/
所以,在Ecmascript 6:
let values = [1,2,3,4,5];
let evens = values.filter(v => v % 2 == 0);
alert(evens);
https://jsfiddle.net/emrefatih47/nnn3c2fo/
我的版本user113716的答案。如果没有找到匹配,他就会删除一个值,这是不好的。
var y = [1, 2, 3]
var removeItem = 2;
var i = $.inArray(removeItem,y)
if (i >= 0){
y.splice(i, 1);
}
alert(y);
如果找到匹配项则删除1项,如果没有匹配项则删除0项。
工作原理:
美元。inArray(value, array)是一个jQuery函数,用于查找数组中值的第一个索引 如果未找到值,则返回-1,因此在执行删除之前检查i是否是有效的索引。删除索引-1意味着删除最后一个,这在这里没有帮助。 .splice(index, count)删除从index开始的count个值,所以我们只需要1的计数