The Wayback Machine - https://web.archive.org/web/20250126110455/https://www.geeksforgeeks.org/remove-elements-from-a-javascript-array/
Open In App

Remove Elements From a JavaScript Array

Last Updated : 27 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Here are the various methods to remove elements from a JavaScript Array

Remove-Array-Elements

Remove elements from Array

1. Using pop() method

The pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.

javascript
let a = ["Apple", "Banana", "Orange", "Mango"];

// Removing the last element
let pop = a.pop();

console.log("Removed Element: ", pop);
console.log("Updated Array: ", a);

Output
Removed Element:  Mango
Updated Array:  [ 'Apple', 'Banana', 'Orange' ]

Please Read JavaScript Array Tutorial for complete understanding of JavaScript Array.

2. Using shift() Method

The shift() method is used to remove and return the first element of the array and reduce the size of the original array by 1. 

javascript
let a = ["Apple", "Banana", "Orange", "Mango"];

// Removing the first element
let fEle = a.shift();

console.log("Removed Element: ", fEle);
console.log("Updated Array: ", a);

Output
Removed Element:  Apple
Updated Array:  [ 'Banana', 'Orange', 'Mango' ]

3. Using splice() Method

The splice() method is used to modify the contents of an array by removing the existing elements and/or adding new elements.

javascript
let a = ["Apple", "Banana", "Orange", "Mango"];

// Removing the specified element
let rEle = a.splice(1, 1);

console.log("Removed Element: ", rEle);
console.log("Updated Array: ", a);

Output
Removed Element:  [ 'Banana' ]
Updated Array:  [ 'Apple', 'Orange', 'Mango' ]

4. Using filter() Method

If we need to remove elements based on a condition, the filter() method is best option. It creates a new array with only the elements that meet the condition.

javascript
function isPositive(val) {
    return val > 0;
}

let a = [10, 25, 30, -10, 32, -35];
let filtered = a.filter(isPositive);

console.log("Positive Array Elements: ", filtered);

Output
Positive Array Elements:  [ 10, 25, 30, 32 ]

5. Using Delete Operator

The delete operator returns a boolean value true, if the element or property is removed from the array or object and false, if a function or variable is passed to remove.

javascript
let a = ["Apple", "Banana", "Orange", "Mango"];

// Delete Element at Index 2
let deleted = delete a[2];

console.log("Removed Element: ", deleted);
console.log("Updated Array: ", a);

Output
Removed Element:  true
Updated Array:  [ 'Apple', 'Banana', <1 empty item>, 'Mango' ]

6. Using Array Reset

Removing the elements from an array using the manual clear and reset approach either by resetting the length of the array as 0 using the length property or by assigning the array to an empty array([]).

javascript
let a1 = ["Apple", "Banana", "Orange", "Mango"];
let a2 = [10, 20, 30, 40, 50];

// After Deleting Each Element of an Array
a1 = [];
a2.length = 0;

console.log("Updated Array1: ", a1);
console.log("Updated Array2: ", a2);

Output
Updated Array1:  []
Updated Array2:  []

7. Using for() Loop and New Array

A simple for loop will be run over the array and pushes all elements in the new array except the element that has to be removed.

JavaScript
let rEle = (a, n) => {
    let newA = [];

    for (let i = 0; i < a.length; i++) {
        if (a[i] !== n) {
            newA.push(a[i]);
        }
    }

    return newA;
};

// Driver Code
let a = [10, 20, 30, 40, 50];
let rElement = 20;

let res = rEle(a, rElement);

console.log("Updated Array: ", res);

Output
Updated Array:  [ 10, 30, 40, 50 ]

8. Using lodash _.remove() Method

We can remove the array elements using Lodash library. The lodash _.remove() method is used to remove the element from array. To use the lodash library, you need to install it locally on your system.

JavaScript
// Import Lodash Library
const _ = require('lodash');

// Declare and Initialize an Array
let a = [101, 98, 12, -1, 848];

// Using _.remove() Method to Remove Odd Number
let even= _.remove(a, function(n) {
    return n % 2 == 0;
});

console.log("Remaining Odd Elements: ", a);
console.log("Removed Even Elements: ", even);  

Output

Removed Odd Elements: 101, -1
Remaining Even Elements: 98, 12, 848

9. Using forEach() and splice() Methods

This method uses forEach to iterate over the array and indexOf to find the index of the element that needs to be removed. The splice method is then used to remove the element at that index.

JavaScript
// Function to remove specific element from a
function remEt(a, ele) {
    a.forEach((item, index) => {
        if (item === ele) {
            a.splice(index, 1);
        }
    });
    return a;
}

// Declare and Initialize an Array
let a = ["Apple", "Banana", "Orange", "Mango"];

// Remove Specific Item from Array
remEt(a, "Banana");

console.log("Updated Array: ", a);

Output
Updated Array:  [ 'Apple', 'Orange', 'Mango' ]


Next Article

Similar Reads

three90RightbarBannerImg