Different ways to delete an item from an array using JavaScript
Last Updated :
28 Nov, 2023
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.
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]);
}
}
console.log(arr);
console.log(arrayWithoutGFG);
|
Output
[ 'gfg', 'GFG', 'g', 'GeeksforGeeks' ]
[ 'gfg', 'g', 'GeeksforGeeks' ]
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();
|
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();
|
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" );
console.log(removed);
console.log(myFruit.length);
console.log(myFruit);
}
myFunc();
|
Output
[ 'grapes', 'strawberry' ]
3
[ 'apple', 'banana', 'guava' ]
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));
|
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 ]
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 ]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...