Different ways to delete an item from an array using JavaScript
Last Updated :
03 Jul, 2024
In Javascript, we do not have any array.remove() method for deleting the element. we will have an array and we need to delete a given item from that array and return the resulting array in the console.

These are the following methods for solving this problem:
Note: There are some other methods that are created by JavaScript inbuilt methods.
Method 1: Using for loop and push() Method
This method will not mutate the original array. First, you have to create an empty() array and then loop over the new array and push only those elements that you want.
Example: This example shows the above-explained approach.
JavaScript
let arr = ['gfg', 'GFG', 'g', 'GeeksforGeeks'];
const arrayWithoutGFG = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== 'GFG') {
arrayWithoutGFG.push(arr[i]);
}
}
// arr is same
console.log(arr);
console.log(arrayWithoutGFG);
Output[ 'gfg', 'GFG', 'g', 'GeeksforGeeks' ]
[ 'gfg', 'g', 'GeeksforGeeks' ]
Method 2: Using Pop() Method
This method is used to delete the last element of the array and return the deleted item as an output. Removing the element decreases the length of the array.
Example: In this example, the pop() method is used for deleting the element of an array.
JavaScript
function myFunc() {
let arr = ['gfg', 'GFG', 'g', 'GeeksforGeeks'];
let name = arr.pop();
console.log(name);
console.log(arr.length)
}
myFunc();
Method 3: Using shift() Method
This method is used to delete an element from the beginning of an array. This method is used to return the first element of an array. It also decreases the length of the original array.
Example: In this example, the shift() method is used for deleting the first element of an array.
JavaScript
function myFunc() {
let arr = ['gfg', 'GFG', 'g', 'GeeksforGeeks'];
let name = arr.shift();
console.log(name);
console.log(arr.length)
}
myFunc();
Method 4: Using splice() Method
This method is used for deleting the existing element or replacing the content of the array by removing/adding a new element.
Example: In this example, the splice method will be used to delete the item from an array.
JavaScript
function myFunc() {
let myFruit = ["apple", "banana", "grapes", "strawberry"];
const removed = myFruit.splice(2, 2, "guava");
// Removed element in the array
console.log(removed);
// Length of the original array after deleting
console.log(myFruit.length);
// Original array after deleting the array
console.log(myFruit);
}
myFunc();
Output[ 'grapes', 'strawberry' ]
3
[ 'apple', 'banana', 'guava' ]
Method 5: Using filter() Method
This method returns the new array. Those array element that satisfies the condition of function is only passed on to the new array. This method does not change the original array.
Example: In this example, we will use the filter() method to delete an item from an array.
JavaScript
const arr = [2, 7, 9, 15, 19];
function isPrime(n) {
for (let i = 2; n > i; i++) {
if (n % i === 0) {
return false;
}
}
return n > 1;
}
console.log(arr.filter(isPrime));
Method 6: Using delete Operator
This operator is more specifically used to delete JavaScript object properties.
Example: In this example, we will use JavaScript delete operator to delete items from an array.
JavaScript
const arr = [2, 7, 9, 15, 19];
delete arr[3];
console.log(arr);
Output[ 2, 7, 9, <1 empty item>, 19 ]
Method 7: Using Lodash _.remove() Method
The _.remove() method is used to remove all elements from the array that predicate returns True and returns the removed elements.
Example: This example shows the above-explained approach.
JavaScript
const _ = require("lodash");
let arr = [1, 2, 3, 4, 5];
let even = _.remove(arr, function (n) {
return n % 2 == 0;
});
console.log('Original Array ', arr);
console.log('Removed element array ', even);
Output:
Original Array [ 1, 3, 5 ]
Removed element array [ 2, 4 ]
Using Lodash _.pullAt method
Using Lodash’s _.pullAt method removes elements from an array at specified indices. It modifies the original array, making it efficient for targeted deletions. This method takes the array and an array of indices to remove, returning the removed elements.
Example:
JavaScript
const _ = require('lodash');
let array = ['a', 'b', 'c', 'd', 'e'];
// Remove elements at specific indices
_.pullAt(array, [1, 3]);
console.log(array);
Output:
['a', 'c', 'e']
Please Login to comment...