How to Remove an Element from an Array in JavaScript?

Spread the love
Skylinewebz

Fundamental JavaScript procedure, removal of members from an array is necessary for data manipulation, filtering, and transformation. This post will investigate several ways to effectively remove elements from an array, therefore improving your knowledge and capacity to manage arrays.

1. Using pop() method

The last element of the array is deleted and returned using thepop() method. This operation shrinks the array’s length by 1.

JavaScript

// JavaScript code to illustrate pop() function
// to remove array elements

let arr = ["shift", "splice", "filter", "pop"];

// Popping the last element from the array
let popped = arr.pop();
console.log("Removed element: " + popped);
console.log("Remaining elements: " + arr);

Output

Removed element: pop
Remaining elements: shift,splice,filter

2. Using shift() Method

The first element of the array is eliminated using the shift() technique, therefore shrinking the original array by 1. 

JavaScript

// JavaScript code to illustrate shift() method
// to remove elements from array
let arr = ["shift", "splice", "filter", "pop"];

// Removing the first element from array
let shifted = arr.shift();
console.log("Removed element: " + shifted);
console.log("Remaining elements: " + arr);

Output

Removed element: shift
Remaining elements: splice,filter,pop

3. Using splice() Method

Any specific element from an array in JavaScript can be deleted with the JavaScript Array splice() technique. Furthermore, this ability allows one to add or remove multiple elements from an array.

Syntax

array.splice(index, howmany, item1, ....., itemX)

Javascript

let Arr = ["C++ ", " Java ", " Python ", " Go ", " Prolog"];

Arr.splice(2, 1);
console.log(Arr);

Output

[ 'C++ ', ' Java ', ' Go ', ' Prolog' ]

4. Using filter() Method

This approach generates a new array from a given array comprising just those elements satisfying a criteria defined by the argument function JavaScript.

// JavaScript to illustrate filter() method
function isPositive(value) {
    return value > 0;
}

let filtered = [101, 98, 12, -1, 848].filter(isPositive);
console.log("Positive elements in array: " + filtered);

Output

Positive elements in array: 101,98,12,848

5. Using Delete Operator

Use the Delete Operator to remove elements from a JavaScript array. JavaScript

// Declare and initialize an array
let array = ["lodash", "remove", "delete", "reset"]

// Delete element at index 2
let deleted = delete array[2];

console.log("Removed: " + deleted);
console.log("Remaining elements: " + array);

Output

Removed: true
Remaining elements: lodash,remove,,reset

6. Using _.remove() Method

The _.remove() method is used to remove all elements from the array that predicate returns True and returns the removed elements.

let _ = require("lodash");
let array = [101, 98, 12, -1, 848];
let evens = _.remove(array, function (n) {
    return n % 2 == 0;
});

console.log("odd elements: " + array);
console.log("even elements: " + evens);

Output

odd elements: 101, -1
even elements: 98, 12, 848

7. Using Array.prototype.slice() Method

One might generate a new array excluding the elements you wish to delete by means of the slice() method While slice() returns a new array rather than changing the original one like splice() does,.

Syntax

array.slice(start, end);

JavaScript

function removeElementAtIndex(arr, index) {
    return arr.slice(0, index).concat(arr.slice(index + 1));
}

const array = [1, 2, 3, 4, 5];
const indexToRemove = 2;

const newArray = removeElementAtIndex(array, indexToRemove);
console.log(newArray);

Output

[ 1, 2, 4, 5 ]