var arr = [ "alice","bob","charli","dane","elisha","furnos"]; var temp = "bob";
I want to remove bob from arr by using variable temp.
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.
filter will iterate the entire array no matter how many items you have. OK nowadays in V8 loops are extremely fast so we don't need to care much. But just saying.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);