JavaScript Remove Index From Array

  1. Use the splice() Function to Remove a Specific Element From JavaScript Array
  2. Use Array.filter() to Remove a Specific Element From JavaScript Array
  3. Use the Underscore.js Library to Remove a Specific Element From JavaScript Array
  4. Use the Lodash Library to Remove a Specific Element From JavaScript Array

This tutorial teaches how to remove a specific element from an array in JavaScript.

Use the splice() Function to Remove a Specific Element From JavaScript Array

The splice() method can modify the array’s content by adding/removing elements. It takes the following 3 arguments :

  1. index: An integer value specifying the position to add/remove elements. We can even specify an index from the back of the array by using negative indices.
  2. howmany: It is an optional parameter. It specifies how many items will be removed from the array. If it is set to 0, then no items are removed.
  3. item1, item2, ... ,itemx: The items to be added to the array.
const array = [1, 2, 3, 4, 5];
const index = array.indexOf(3);
if (index > -1) {
  array.splice(index, 1);
}
console.log(array); 

Output:

[1, 2, 3, 5]

In the above code, we first find the index of the element we want to remove and then use the splice() method to remove the array element.

Use Array.filter() to Remove a Specific Element From JavaScript Array

The filter methods loop through the array and filter out elements satisfying a specific given condition. We can use it to remove the target element and keep the rest of them. It helps us to remove multiple elements at the same time.

var toRemove = 1;
var arr = [1, 2, 3, 4, 5];
arr = arr.filter(function(item) {
    return item !== toRemove
});
console.log(arr)

Output:

[2, 3, 4, 5]

We use the filter function to keep every element that is not equal to the element to be removed and assign the newly formed array to the original array.

Use the Underscore.js Library to Remove a Specific Element From JavaScript Array

Underscore.js is a very helpful library that provides us a lot of useful functions without extending any of the built-in objects. To remove target elements from a JavaScript array, we have to use the without() function. This function returns a copy of the array with all copies of the target element removed.

const arr = [1, 2, 1, 0, 3, 1, 4];
arr = _.without(arr, 0, 1);
console.log(arr);

Output:

[2, 3, 4]

In the above code, we pass the array and the elements to be removed 0 and 1 to the without function. It returns a new array with these elements removed, which we store again in arr.

Use the Lodash Library to Remove a Specific Element From JavaScript Array

Lodash is a great library that allows us to import only the functions we need and not the complete library. It has a function named remove(), which can remove a specific element from the array. This function takes the array and a function to check for conditions matching the element to be removed from the array.

var arr = [1, 2, 3, 4];
var greater = _.remove(arr, function(n) { return n > 2;});
console.log(arr)

Output:

[1,2]

In the above code, we pass the array and a function checking if elements are greater than 2 to the lodash library’s remove function. It removes all elements greater than 2 from the array.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - JavaScript Array

  • Empty an Array in JavaScript
  • Sum of an Array in JavaScript