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
How to remove given object from an Array in Java? Given an array arr of N objects, the task is to remove all the occurrences of a given object from the array in Java. Example: Input: String[] arr = { "Geeks", "for", "Geeks", "hello", "world" }, removeObj = "Geeks" Output: updated arr[] = {"for", "hello", "world"}Explanation: All the occurrences of
3 min read
JavaScript - Delete Elements from the End of a JS Array These are the following ways to delete elements from the end of JavaScript arrays:1. Using pop() methodThe pop() method is used to remove the last element from an array and returns the modified array.JavaScriptlet a = [1, 2, 3, 4]; // Remove last element a.pop(); console.log(a);Output[ 1, 2, 3 ] 2.
2 min read
JavaScript- Delete from a Given Position of JS Array These are the following ways to delete an item from the given array at given position: 1. Using the splice() Method(Most efficient for in-place modifications)The splice() method is the most versatile way to remove elements from any position in an array. It allows you to specify the index where the r
2 min read
How to remove specific elements from the left of a given array of elements using JavaScript ? In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met
2 min read
Remove All Occurrences of an Element in an Array Given an integer array arr[] and an integer ele the task is to the remove all occurrences of ele from arr[] in-place and return the number of elements which are not equal to ele. If there are k number of elements which are not equal to ele then the input array arr[] should be modified such that the
6 min read