JavaScript - How to Remove an Element from an Array?
Last Updated :
26 Nov, 2024
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 pop() method
Thepop() method is used to remove the last element of the array and returns the removed element. This function decreases the length of the array 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);
OutputRemoved element: pop
Remaining elements: shift,splice,filter
2. Using shift() Method
The shift() method is used to remove the first element of the array and reduce the size of 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);
OutputRemoved element: shift
Remaining elements: splice,filter,pop
3. Using splice() Method
The JavaScript Array splice() method can be used to remove any particular element from an array in JavaScript. Moreover, this function can be used to add/remove more than one element 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 method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set 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);
OutputPositive 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);
OutputRemoved: 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.
JavaScript
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
The slice() method can be used to create a new array that excludes the elements you want to remove. Unlike splice(), which modifies the original array, slice() does not alter the original array but returns a new one.
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);
Similar Reads
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 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
How to Remove a Specific Item from an Array in JavaScript ? Given an Array, the task is remove specific item from an array in JavaScript. It means we have an array with N items, and remove a particular item from array.ExamplesInput: Arr = [ 10, 20, 30, 40, 50, 60 ] removeItem = 40 Output: [ 10, 20, 30, 50, 60 ]Table of ContentUsing splice() MethodUsing filte
3 min read
JavaScript - Delete an Item From an Array 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() MethodThe push() method will not mutate the
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