jQuery Remove Elements From array and Object

jQuery remove elements from an array. Through this tutorial, i am going to show you how to remove elements or items from array and object in jQuery.

How to Remove Elements From Array and Array Object in jQuery

Use the the jquery grep() method to remove elements from array and object in jQuery:

  • Syntax of jQuery $.grp() method
  • Parameters of jQuery grp() Method
  • Example 1 – jQuery Remove Element From Array
  • Example 2 – jQuery Remove Numeric Element From Array
  • Example 3 – jQuery Remove Element From Array By Value
  • Example 4 – jQuery Remove Element From Array Object with Key Value

Syntax of jQuery $.grp() method

$.grep( array, function [, invert ] );

Parameters of jQuery grp() Method

ParameterDescription
arrayThis is the first and required Parameter.
function(n,i)It is a second and it also required Parameter: It is the filter function that processes each item of the array.
[, invert ]This is the last and optional Parameter. It Accepts Boolean values

This tutorial has the purpose to show you, how you can easily remove items or elements from an array in jQuery. with several examples.

Here we will take several examples to remove the elements or items from array in jQuery.

Example 1 – jQuery Remove Element From Array

For example; you have array [ ‘php’,’java’,’c’,’laravel’,’.net’,’codeigniter’ ] and want to remove .net from array; as shown below:

  var arr = [ 'php','java','c','laravel','.net','codeigniter' ];
  res = jQuery.grep(arr, function( a ) {
    return a !== '.net';
  });
   
  console.log(res);

Result of the above code is: [“php”, “java”, “c”, “laravel”, “codeigniter”]

Example 2 – jQuery Remove Numeric Element From Array

Let, you have numeric array [1, 2, 3, 4, 5, 6, 7, 8, 9] and want to remove 8 from array. As shown below:

  var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  res = jQuery.grep(arr, function( a ) {
    return a !== 8;
  });
   
  console.log(res);

Result of the above code is: [1, 2, 3, 4, 5, 6, 7, 9]

Example 3 – jQuery Remove Element From Array By Value

Suppose you have a numeric array [1, 2, 3, 4, 5, 6, 7, 8, 9] and want to remove the value before 5 from the array. As shown below:

  var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
   res = $.grep(arr, function (n, i) {
       return (n > 5);
   });

Result of the above code is: [6, 7, 8, 9]

Example 4 – jQuery Remove Element From Array Object with Key Value

Suppose, you have an array with key and value as shown below:

var array = [
    {name:'hello',age:20}, 
    {name:'test',age:25},
    {name:'world',age:21},
    {name:'class',age:19}
];
var array = [
    {name:'hello',age:20}, 
    {name:'test',age:25},
    {name:'world',age:21},
    {name:'class',age:19}
];
var res = jQuery.grep(array, function( n, i ) {
  return ( n.age !== 25 );
});
console.log(res);

Result of the above code is:

0: {name: “hello”, age: 20}
1: {name: “world”, age: 21}
2: {name: “class”, age: 19}

Recommended jQuery Tutorials

Leave a Comment