jQuery Remove Elements From Array

jQuery Remove Elements From Array

jQuery remove element from an array; In this tutorial, You will learn how you how to remove elements or items from array in jQuery.

How to Remove Element Value from Array By Id, Key,Class,Name, Value,Index in jQuery

You can use the jQuery $.grep() method, which is used to remove the elements or items value from array or array object in jQuery.

Syntax of $.grp() method is:

$.grep( array, function [, invert ] );
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 value from array in jQuery.

Example 1

You have an array [ ‘php’,’java’,’c’,’laravel’,’.net’,’codeigniter’ ] and you want to remove .net from the array. Let’s see the below example:

  var arr = [ 'php','java','c','laravel','.net','codeigniter' ];

  res = jQuery.grep(arr, function( a ) {
    return a !== '.net';
  });
   
  console.log(res);

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

Example 2

In this example you have numeric array [1, 2, 3, 4, 5, 6, 7, 8, 9] and you want to remove 8 from array. Let’s see the below example:

  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

In this example, 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. Let’s look at the examples 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

In this example, you have an array with key and value looks like 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}

Example 5

In this example, you have a numeric array [1, 2, 3, 4, 5, 6, 7, 8, 9] and we want to remove the value before 2 from the array. And want to remove the value after 5.

In other words, Get the value from between two numbers and remove all the elements from the array. Let’s look at the examples given below.

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var res = $.grep(arr, function (n, i) {
    return (n > 1 && i < 5);
});

console.log(res);

The result of the above code is: [2, 3, 4, 5]

Recommended jQuery Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *