Wednesday, September 25, 2024
Google search engine
HomeLanguagesJavascriptDifferent ways to delete an item from an array using JavaScript

Different ways to delete an item from an array using JavaScript

In Javascript, we do not have any array.remove() method for deleting the element. Instead of any method Javascript, have different ways to delete an item from an array.

In this article, we will see different ways to delete an item from an array in Javascript.

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 element which you want.

Example:

Javascript




let arr = ['gfg', 'GFG', 'g', 'neveropen'];
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', 'neveropen' ]
[ 'gfg', 'g', 'neveropen' ]

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', 'neveropen'];
    let name = arr.pop();
    console.log(name);
    console.log(arr.length)
}
myFunc();


Output

neveropen
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', 'neveropen'];
    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.

Step to install:

npm install lodash

Example:

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 ]

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments