volvereabhi
volvereabhi

Reputation: 60

How to remove element from array in jquery

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

Answers (3)

jacob13smith
jacob13smith

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

Gangadhar Gandi
Gangadhar Gandi

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

Bloatlord
Bloatlord

Reputation: 393

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

Upvotes: 1

Related Questions