Javascript: Remove array element by index

While working in javascript, often there is a requirement to delete an element from an array by index. This article will discuss removing an element from the javascript array by an index value.

Table of Contents:-

Remove an element from array by index using splice()

Javascript’s splice(start, deleteCount, item1, item2….) method is used to modify the elements of an array. The splice() method can remove, replace or/and add new elements to the array.

  • start: is the index from where the change in the array needs to be done.
  • deleteCount: is the number of elements to be removed from the array. This argument is optional.
  • item1,item2,… : are the elements that need to be added. These arguments are optional.
  • Note that the index in array start from 0

Example:-

Remove the element at 3rd index position from array [1,4,9,16,25]

Code:-

let indexForRemoval = 3;
let numArray = [1,4,9,16,25];
numArray.splice(indexForRemoval,1);
console.log("Array Elements After Removing Element At Index: " + indexForRemoval + " is " + numArray);

Output:-

Array Elements After Removing Element At Index: 3 is 1,4,9,25

Explanation:-

  • The above code uses the splice() method to delete only 1 occurrence of element at 3rd index position.
  • The splice(indexForRemoval,1) method will remove 1 (second argument) element starting from the indexForRemoval (first argument) which holds value 3.

Remove an element from array by index using filter()

Javascript’s filter() method creates a new array that will have only qualified elements after implementing a provided function.

Example:-

Remove the element at 3rd index position from array [1,4,9,16,25]

Code:-

let numArray = [1,4,9,16,25];
let indexForRemoval = 3;
let valueToRemove = [numArray[indexForRemoval]];
numArray = numArray.filter(element => !valueToRemove.includes(element));
console.log("Array Elements After Removing Element At Index: " + indexForRemoval + " is " + numArray);

Output:-

Array Elements After Removing Element At Index: 3 is 1,4,9,25

Explanation:-

  • The above code uses the filter() method to delete the element at the 3rd index position in numArray.
  • The valueToRemove array has only one element which we need to remove. It’s the element at the 3rd index position with a value of 16.
  • Then the filter() method is applied to all the elements of the numArray, which says to get only those elements that do not include elements of the valueToRemove array.
  • Note that the index in array start from 0

Remove an element from array by index using concat() and slice()

Javascript’s slice(startIndex, endIndex) method returns a shallow copy of a portion of the calling array into a new array object based on indexes passed as arguments. The index is zero-based.

  • startIndex: is the index to start the extraction including the value at startIndex position.
  • endIndex: is the index to end the extraction excluding the value at endIndex position. This argument is optional; therefore, if not present, it means to extract array elements till the end of the array.

Javascript’s concat() method is used to merge more than one array and return a new array. The concat() method does not modify the arrays which we are merging.

Example:-

Remove the element at 3rd index position from array [1,4,9,16,25]

Code:-

let indexForRemoval = 3;
let numArray = [1,4,9,16,25];
numArray = numArray.slice(0,indexForRemoval).concat(numArray.slice(indexForRemoval+1))
console.log("Array Elements After Removing Element At Index: " + indexForRemoval + " is " + numArray);

Output:-

Array Elements After Removing Element At Index: 3 is 1,4,9,25

Explanation:-

  • The above code uses the slice() method to divide the original array into two parts.
  • The first part will have elements from 0 till element before the 3rd index.
  • The second part will have elements from the 4th index position till the end.
  • Finally, concat both the arrays and assign them to numArray.
  • Note that the indexe in array start from 0

Read More:

I hope this article helped you to remove an element from an array by index position in javascript. Good Luck !!!

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top