How To Remove Specific Item From Array In Javascript

Websolutionstuff | Nov-03-2022 | Categories : jQuery

In this article, we will see how to remove a specific item from an array in javascript. We will use the indexOf() method and splice() method to remove the item from the array at a specific index in javascript. Here, we will find the index of the array you want to remove using the indexOf() method after that remove the index using the splice() method. Also, we will remove specific values from an array using the filter() method.

The indexOf() method returns the first index of the value in the string. The indexOf() returns -1 if value is not present in the string.

The splice() method is used to change the content of the array by removing or replacing an existing element and/or adding new elements. The splice() method overwrites the original array element.

The filter() method creates a duplicate of the given array. The filter() method doesn't change the original array and it doesn't execute the function for an empty array element.

So, let's see how to remove a specific item from an array in jquery.

Javascript Remove Specific Item From The Array

Example:

const array = [2, 3, 5];

console.log(array);

const index = array.indexOf(3);
if (index > -1) { // only splice array when item is found
  array.splice(index, 1); // 2nd parameter means remove one item only
}

// array = [2, 5]
console.log(array); 

Output:

[ 2, 3, 5 ]

[ 2, 5 ]

 

 

Example:

The first function removes a single occurrence like removing the first match of 3 in a given array value [2,3,5,7,3,9,3]. And the second function is to remove all occurrences in a given array value.

function removeItemOnce(arr, value) {
    var index = arr.indexOf(value);
    if (index > -1) {
      arr.splice(index, 1);
    }
    return arr;
  }
  
  function removeAllItem(arr, value) {
    var i = 0;
    while (i < arr.length) {
      if (arr[i] === value) {
        arr.splice(i, 1);
      } else {
        ++i;
      }
    }
    return arr;
  }
  // Usage
  console.log(removeItemOnce([2,3,5,7,3,9,3], 3))
  console.log(removeAllItem([2,3,5,7,3,9,3], 3))

Output:

[ 2, 5, 7, 3, 9, 3 ]

[ 2, 5, 7, 9 ]

 

Example:

var value = 3

var arr = [2,3,5,7,3,9,3]

arr = arr.filter(function(item) {
    return item !== value
})

console.log(arr)

Output:

[2, 5, 7, 9]

 


You might also like:

Recommended Post
Featured Post
Laravel 9 One To Many Polymorphic Relationship
Laravel 9 One To Many Polymorp...

In this article, we will see laravel 9 one to many polymorphic relationship. A one-to-many polymorphic relation is...

Read More

Apr-05-2022

How To Fix cURL Error 60 SSL Certificate Problem
How To Fix cURL Error 60 SSL C...

In this example we see how to fix cURL error 60 SSL certificate problem. cURL error 60: SSL certificate proble...

Read More

Dec-08-2021

How to Image Upload with Preview in Angular 17
How to Image Upload with Previ...

In this article, we'll see how to image upload with a preview in angular 17. Here, we'll learn about the an...

Read More

Apr-01-2024

Laravel 10 Delete Multiple Records Using Checkbox
Laravel 10 Delete Multiple Rec...

In this article, we will see laravel 10 delete multiple records using the checkbox. Here, we will learn about how to del...

Read More

Mar-03-2023