How to remove specific item from an array using jQuery?

September 28, 2022 No comments javascript js jquery array remove item tips solutions

Introduction

In JavaScript, there are situations when we want to remove items from an array using jQuery.

In this article, we presented a method how to do it.

How to remove specific item from an array using jQuery

To remove specific item from an array using jQuery we need to use jQuery.grep(...) method.

This function identifies the array members that meet a specified filter function.

Note that: there is no change to the initial array.

let array = [1, 2, 3, 2, 2, 4];
let removeItem = 2;

array = jQuery.grep(y, function(value) {
  return value != removeItem;
});

console.log(array); // [1, 3, 4]

or

let array = [1, 2, 3];
let removeItem = 2;

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

console.log(array); // [1, 3]

Conclusion

To remove specific item/items from an array using jQuery we can use jQuery.grep(...) method that works similarly to ES6 array.filter(...) function.

Another method is to use the native .splice() function and jQuery's $.inArray().

{{ message }}

{{ 'Comments are closed.' | trans }}