Reputation: 60
var arr = [ "alice","bob","charli","dane","elisha","furnos"]; var temp = "bob";
I want to remove bob from arr by using variable temp.
Upvotes: 0
Views: 1139
Reputation: 929
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.
Upvotes: 3
Reputation: 2252
We can use Javascript array's filter method to remove the required item.
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,
var arr = ["alice", "bob", "charli", "dane", "elisha", "furnos"];
var temp = "bob";
arr = jQuery.grep(arr, function (value) {
return value != temp;
});
console.log(arr);
Upvotes: 1