JavaScript - Delete an Item From an Array
Last Updated :
07 Jan, 2025
In Javascript, we do not have any array.remove() method for deleting the element. Here are the various methods we can delete an item from an array using JavaScript.
Different Ways to Delete an Item From an Array Using JavaScript1. Using for loop and push() Method
The push() 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.
JavaScript
let a1 = ['gfg', 'GFG', 'g', 'GeeksforGeeks'];
const a2 = [];
for (let i = 0; i < a1.length; i++) {
if (a1[i] !== 'GFG') {
a2.push(a1[i]);
}
}
console.log(a1);
console.log(a2);
Output[ 'gfg', 'GFG', 'g', 'GeeksforGeeks' ]
[ 'gfg', 'g', 'GeeksforGeeks' ]
In this example
- It uses a for loop to iterate through arr.
- If an element is not 'GFG', it's added to a2.
- Finally, it prints both the original arr and the modified a2.
2. Using Pop() Method
Pop() method is used to delete the last element of the array and return the deleted item as an output.
JavaScript
let a1 = ['gfg', 'GFG', 'g', 'GeeksforGeeks'];
let a2 = a1.pop();
console.log(a2);
console.log(a1.length)
In this example
arr.pop()
removes and returns the last element of the array ('GeeksforGeeks'
).- The new length of
arr
(after removal) is logged.
3. Using shift() Method
Shift() method is used to delete an element from the beginning of an array.
JavaScript
let a1 = ['gfg', 'GFG', 'g', 'GeeksforGeeks'];
let a2 = a1.shift();
console.log(a2);
console.log(a1.length)
In this example
- a1.shift() removes and returns the first element of the array ('gfg').
- The new length of arr (after removal) is logged.
4. Using splice() Method
Splice() method is used for deleting the existing element or replacing the content of the array by removing/adding a new element.
JavaScript
let a = ["apple", "banana", "grapes", "strawberry"];
const rem = a.splice(2, 2, "guava");
console.log(rem);
console.log(a.length);
console.log(a);
Output[ 'grapes', 'strawberry' ]
3
[ 'apple', 'banana', 'guava' ]
In this example
- a.splice(2, 2, "guava"): This removes 2 elements from index 2 ("grapes" and "strawberry") and adds "guava" in their place.
- The removed elements are stored in rem, and a.length reflects the new size of the array after the change.
5. Using filter() Method
filter() method returns the new array. Those array element that satisfies the condition of function is only passed on to the new array.
JavaScript
const a = [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(a.filter(isPrime));
In this example
- isPrime(n): Returns true if n is prime, otherwise false.
- a.filter(isPrime): Filters array and returns a new array containing only the prime numbers from array.
6. Using delete Operator
Delete operator is more specifically used to delete JavaScript object properties.
JavaScript
const a = [2, 7, 9, 15, 19];
delete a[3];
console.log(a);
Output[ 2, 7, 9, <1 empty item>, 19 ]
- delete arr[3]: Removes the element at index 3 (value 15), but the array length remains unchanged, and the index is left with an undefined value.
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.
JavaScript
const _ = require("lodash");
let a = [1, 2, 3, 4, 5];
let even = _.remove(a, function (n) {
return n % 2 == 0;
});
console.log('Original Array ', a);
console.log('Removed element array ', even);
Output:
Original Array [ 1, 3, 5 ]
Removed element array [ 2, 4 ]
In this example
- _.remove(a, function(n) { return n % 2 == 0; }): Removes all even numbers from a and returns the removed elements in a new array (even).
- a: The original array is modified in place.
- even: Contains the even numbers that were removed.
8. 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.
JavaScript
const _ = require('lodash');
let a = ['a', 'b', 'c', 'd', 'e'];
_.pullAt(a, [1, 3]);
console.log(a);
Output:
['a', 'c', 'e']
In this example
- _.pullAt(a, [1, 3]): This removes the elements at indices 1 and 3 from the array (i.e., 'b' and 'd').
- The function modifies the original array directly, removing the specified elements.
Similar Reads
JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po
3 min read
JavaScript - Delete Elements from an Index in JS Array These are the following ways to delete elements from a specified Index of JavaScript arrays:1. Using splice() MethodThe splice() method is used to remove elements directly from a specified index in an array by specifying the index and the number of elements to delete.JavaScriptlet a = [1, 2, 3, 4, 5
1 min read
How to delete an element from an array using JavaScript ? The array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index. In this article, we will discuss different ways to remove elements from the array.
5 min read
JavaScript- Delete from a Given Position of JS Array These are the following ways to delete an item from the given array at given position: 1. Using the splice() Method(Most efficient for in-place modifications)The splice() method is the most versatile way to remove elements from any position in an array. It allows you to specify the index where the r
2 min read
JavaScript - Replace an Item from JS Array These are the following ways to replace an item from the JS array: 1. Using Array IndexingThe Array Indexing approach replaces an item in an array by directly accessing its position using the index and assigning a new value. This method is straightforward, modifies the original array, and is ideal f
2 min read
Delete from a JS Array The following are different delete operations on a JS Array:Delete from the Beginning of JS ArrayThis operation removes the first element of an array, shifting all other elements to lower indices. The array's length is reduced by one. Methods like shift() can be used for this, which directly mutates
3 min read
Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe 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.javascriptlet a = ["Apple",
6 min read
How to Empty an Array in JavaScript? To empty an array in JavaScript, we can use the array literal. We can directly assign an empty array literal to the variable, it will automatically remove all the elements and make the array empty.1. Empty Array using Array LiteralWe can empty the array using the empty array literal syntax. Syntaxar
2 min read
How to Splice Duplicate Item from JavaScript Array? Given an array of numbers or strings containing some duplicate values, the task is to remove these duplicate values from the array without creating a new array or storing the duplicate values anywhere else.Examples: Input: arr = [1, 2, 3, 4, 3, 2, 1];Output: [1,2,3,4]Input: [1, 4, 6, 1, 2, 5, 2, 1,
4 min read
How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i
4 min read