简体   繁体   English

如何从jquery中的数组中删除元素

[英]How to remove element from array in jquery

var arr = [ "alice","bob","charli","dane","elisha","furnos"]; var arr = [ "alice","bob","charli","dane","elisha","furnos"]; var temp = "bob"; var temp = "bob";

I want to remove bob from arr by using variable temp.我想通过使用变量 temp 从 arr 中删除 bob。

This is an easy one liner:这是一个简单的单衬:

arr.splice( arr.indexOf(temp), 1 );

Looks for the variable temp in the array and removes one element at that index.在数组中查找变量temp并删除该索引处的一个元素。

arr.filter((name) => name !== temp);

We can use Javascript array's filter method to remove the required item.我们可以使用 Javascript 数组的 filter 方法来删​​除所需的项目。

 var arr = ["alice", "bob", "charli", "dane", "elisha", "furnos"]; var temp = "bob"; var filteredArray = arr.filter(item => item !== temp); console.log(filteredArray);

OR或者

With Jquery we can go with grep,使用 Jquery 我们可以使用 grep,

var arr = ["alice", "bob", "charli", "dane", "elisha", "furnos"];
var temp = "bob";
arr = jQuery.grep(arr, function (value) {
    return value != temp;
});
console.log(arr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM