The Wayback Machine - https://web.archive.org/web/20240712114852/https://www.geeksforgeeks.org/different-ways-to-delete-an-item-from-an-array-using-javascript/
Open In App

Different ways to delete an item from an array using JavaScript

Last Updated : 03 Jul, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

diff-ways-to-delete-an-item-from-array

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();

Output
GeeksforGeeks
3

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();

Output
gfg
3

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));

Output
[ 2, 7, 19 ]

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']


Similar Reads

Different ways to search an item in an array in JavaScript
Searching for an item in a JavaScript array involves determining whether a specific element is present within the array. This process is fundamental for tasks like data validation, filtering, and manipulation, enabling developers to locate and manage elements within arrays in their applications efficiently. These are the following methods: Table of
4 min read
How to delete an item or object from the array using ng-click ?
The task is to delete the item from the list when the button is clicked. This all should be done by using ng-click. This is done by using the splice() method. The syntax for the method is given below. Syntax for splice() function: array.splice(indexno, noofitems(n), item-1, item-2, ..., item-n) Example for splice() function: const topics = ['Array'
2 min read
Which one method use to delete an element from JavaScript array (slice or delete) ?
We can use both methods to delete an element from a javascript array. The answer is solely dependent on the application and on the programmer. What slice() method does? The slice method, as the name suggests, slices an array and returns a copy of the new array. This method does not affect the main array, it just returns the copy of the array. We ca
4 min read
Delete the array elements in JavaScript | delete vs splice
This article will show you how to Delete the array element at a specific position using JavaScript. There are two approaches that can be used to delete elements in an array. They have their own merits regarding the way they perform the deletion. Delete array[index]:This method deletes the element at the index specified but does not modify the array
3 min read
Get the first and last item in an array using JavaScript
JavaScript array is used to store multiple elements in a single variable. It is often used when we want to store a list of elements and access them by a single variable. We will learn how to get the first &amp; last element in an array in JavaScript, along with understanding their implementation through the examples. These are the following approac
5 min read
Different Ways to Use Array Slice in JavaScript
In this article, we will see the different ways to use the Array slice method in Javascript. Using Array Slice in JavaScript refers to the technique of extracting a specified portion of an array, defined by start and end indices, to create a new array containing those selected elements. Syntaxarr.slice(begin, end);Parameters: This method accepts tw
3 min read
Different Ways to Crate an Array of Objects in JavaScript ?
Objects in JavaScript are key-value pairs, making them suitable for representing structured data. Also, an array in JavaScript is a versatile data structure that can hold a collection of values. When dealing with objects, an array can be used to store multiple objects. An array of objects allows you to organize and manage multiple sets of related i
3 min read
Different ways to populate an array in JavaScript
Populating an array in JavaScript means initializing it with elements. This could involve adding values manually, dynamically generating values using loops or functions, or copying elements from another array. It's a fundamental operation for creating and manipulating arrays in JavaScript programs. These are the following ways to populate an array:
5 min read
Different ways to access HTML elements using JavaScript
Sometimes, users need to manipulate the HTML element without changing the code of the HTML. In this scenario, users can use JavaScript to change HTML elements without overwriting them. Before we move ahead to change the HTML element using JavaScript, users should learn to access it from the DOM (Document Object Model). Here, the DOM is the structur
7 min read
Count Frequency of an Array Item in JavaScript
In this article, we will be counting the frequency of an array item in Javascript. When working with arrays there are situations when we need to count the frequency of an array item. Javascript provides us with some methods to count the occurrences of an item in an array, some of them are explained below: Using for loopUsing filter() methodUsing re
3 min read
three90RightbarBannerImg