jQuery – Remove Elements From array jQuery

11-Apr-2023

.

Admin

Now let's see example of how to remove element from array in jquery. In this article i am going learn you remove specific element from array using jquery. We will show jquery remove all duplicate elements from array. If you want to remove perticuler element from array in jquery then you can use bellow example.

Here i will give four way to remove element from array in jquery. So let's see the bellow all soultion:

Solution 1


var myArray = [1, 2, 2, 3, 2]

var removeItem = 2;

myArray.splice( $.inArray(removeItem, myArray), 1 );

// Output

// [1, 2, 3, 2]

Output

[1, 2, 3, 2]

Solution 2

var a = ['a','b','c','d'];

a.splice(a.indexOf('c'),1);

// Output

// ["a", "b", "d"]

Output

["a", "b", "d"]

Solution 3 : Remove all Duplicate Element

// Remove All Specific Duplicate Items

var myArray = [1, 2, 2, 3, 2]

var removeItem = 2;

myArray = jQuery.grep(myArray, function(value) {

return value != removeItem;

});

// Output

// [1, 3]

Output

[1, 3]

Solution 4 : Remove all Duplicate Element

// Remove All Specific Duplicate Items

var myArray = [1, 2, 2, 3, 2]

var removeItem = 2;

var myArray = myArray.filter(function(elem){

return elem != 2;

});

// Output

// [1, 3]

Output

[1, 3]

It will help you...

#Jquery