How to Remove Element From Array in Javascript

Ruslan Osipov
Author: Ruslan Osipov

There are several ways to remove elements from an array in JavaScript.

remove element from array in javascript

Remove the first element from the array in JavaScript

If you need to remove the first element, use Array.shift(). This method not only removes the element but also returns it.

let a = [0, 1, 2, 3, 4, 6, 7, 8]
const firstElement = a.shift()
console.log(firstElement) // 0
console.log(a) // [1, 2, 3, 4, 6, 7, 8]

Remove the last element from the array in JavaScript

If you need to remove the last element you can use Array.pop() method. This method not only removes the element but also returns it.

const lastElement = a.pop()
console.log(lastElement) // 8
console.log(a) // [1, 2, 3, 4, 5, 6, 7]

Another way to delete the last element(s) id to set the length of the array, that will remove the other elements with indexes more than length.

a.length(4)
console.log(a) // [1, 2, 3, 4]

Remove element from the array by its index

If you know the index of the element you want to remove, you can use Array.splice(start, ?deleteCount) method. The first argument is the start index, the second one is how many elements you want to remove.

const mySlice = a.slice(0,1)
console.log(mySlice) // [1]
console.log(a) // [2, 3, 4]

Remove element from the array by value

In case you need to find the element with specific value you can use Array.splice(start, ?deleteCount) method. First you have to find the element index. We can use simple "for loop":

let array = [0, 1, 2, 3, 4, 6, 7, 8]

for(let index = 0; index < array.length; index++){         
    if ( array[index] === 7) { 
        array.splice(index, 1)
        index-- // make sure you don't forget this
    }
}
console.log(array) //[0, 1, 2, 3, 4, 6, 8]

Another option is to use Array.filter() method, but with this approach a new array will be created:

const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const filteredArray = array.filter((value, index, arr)=>{ 
    return value > 6
});
console.log(filteredArray) [7, 8, 9]
console.log(array) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Other methods

Other methods will be just a fancy ways of doing the same things I described above. You also can use utility libraries, like Lodash (using _.remove) or Underscore (using .findWhere with .without)

Create a helper to remove element from the array by condition

You also can hide the implementation in your own helper:

// This function will modify existing array
function removeItemFromArray(array, condition){
    for(let index = 0; index < array.length; index++){         
        if ( condition(index, array[index])) { 
            array.splice(index, 1)
            index-- // make sure you don't forget this
        }
    }
    return array // optional
}

const array = [
    {name: 'John Dow'},
    {name: 'Alice Dow'},
    {name: 'Mr Beast'},
]

const filteredArray = removeItemFromArray(array, (index, value)=> value.name === 'Mr Beast')
console.log(filteredArray) // [{name: 'John Dow'}, {name: 'Alice Dow'}]

Removing an element from an array in Javascript can be done in multiple ways, depending on your specific needs.

The splice method is useful when you want to remove a specific element by its index, the filter method is great for removing multiple elements that match a certain condition, the pop and shift methods are ideal for removing the first or last element of an array, and the unshift method can be used to remove the first element by creating a new array.

By understanding and utilizing these methods, you can manipulate your Javascript arrays with greater ease and precision.

FAQs

How do I remove an element from an array by its index?

You can use the 'splice' method to remove an element from an array by its index.


Can I remove multiple elements from an array at once?

Yes, you can use the 'splice' method with two parameters to remove multiple elements from an array at once.


How do I remove the last element from an array?

You can use the 'pop' method to remove the last element from an array


How do I remove the first element from an array?

You can use the 'shift' method to remove the first element from an array.


Can I remove the first element from an array without creating a new array?

No, the 'shift' method removes and returns the first element, so you need to save it to a variable or discard it.


How do I remove elements from an array based on a certain condition?

You can use the 'filter' method to create a new array with all elements that match a certain condition, effectively removing the elements that don't match.


Can I remove elements from an array in place, without creating a new array?

Yes, you can use the 'splice' method to remove elements from an array in place, without creating a new array.


How do I remove all elements from an array?

You can set the length of the array to 0, like this: 'array.length = 0;'.


How do I remove the last n elements from an array?

You can use the 'splice' method with a negative index to remove the last n elements from an array.


Can I undo a remove operation on an array?

No, it's gone once you remove an element from an array. You can save a copy of the original array before removing elements, though, and use it to restore the array if needed.