Remove elements from a JavaScript Array
JavaScript array is a single variable that is used to store the elements or a group of values. In this article, we will learn different methods to remove elements from a JavaScript Array. These methods remove elements from an array in any position in JavaScript.
Methods to Remove Elements from JavaScript Array
There are different methods to remove elements from a JavaScript array which are discussed below:Â
Note: There are other methods that are created by JavaScript inbuilt methods.
This 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 every time the element is removed.Â
Example 1:Â The below code is the basic implementation of the pop() method to remove elements from an array.
javascript
let arr = [ "shift" , "splice" , "filter" , "pop" ];
let popped = arr.pop();
console.log( "Removed element: " + popped);
console.log( "Remaining elements: " + arr);
console.log( "Array length: " + arr.length);
|
Output
Removed element: pop
Remaining elements: shift,splice,filter
Array length: 3
Example 2:Â The below code removes the elements from array using pop() method until the length of the array turns to 0.
javascript
let array = [ "pop" , "splice" , "filter" , "shift" ]
console.log( "Original array: " + array)
while (array.length) {
array.pop();
}
console.log( "Array Length: " + array.length)
|
Output
Original array: pop,splice,filter,shift
Array Length: 0
Method 2: Remove First Element from Array using shift() Method
This method is used to remove and return the first element of the array and reduce the size of the original array by 1.Â
Example:Â The below code is a basic implementation of shift() method to remove array elements.
javascript
let arr = [ "shift" , "splice" , "filter" , "pop" ];
let shifted = arr.shift();
console.log( "Removed element: " + shifted);
console.log( "Remaining elements: " + arr);
|
Output
Removed element: shift
Remaining elements: splice,filter,pop
Method 3: Remove Element from Array at any Index using splice() Method
This method is used to modify the contents of an array by removing the existing elements and/or adding new elements. To remove elements by the splice() method you can specify the elements in different ways.Â
Example 1: This example uses the indexing of the splice method to remove elements from a JavaScript array.Â
javascript
let arr = [ "shift" , "splice" , "filter" , "pop" ];
let spliced = arr.splice(1, 1);
console.log( "Removed element: " + spliced);
console.log( "Remaining elements: " + arr);
|
Output
Removed element: splice
Remaining elements: shift,filter,pop
Example 2: This example uses the value of the splice method to remove elements from a JavaScript array.Â
javascript
let arr = [ "shift" , "splice" , "filter" , "pop" ];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === "splice" ) {
let spliced = arr.splice(i, 1);
console.log( "Removed element: " + spliced);
console.log( "Remaining elements: " + arr);
}
}
|
Output
Removed element: splice
Remaining elements: shift,filter,pop
Example 3: Using the splice method to remove each element from a JavaScript array.Â
javascript
let array = [ "pop" , "splice" , "filter" , "shift" ]
console.log( "Original array: " + array)
array.splice(0, array.length);
console.log( "Empty array: " + array)
|
Output
Original array: pop,splice,filter,shift
Empty array:
Method 4: Remove Element from Array with Certain Condition 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. To remove elements by filter() method you can specify the elements in different ways.Â
Example: The below example uses the value of the filter method to remove elements from a JavaScript array.Â
javascript
function isPositive(value) {
return value > 0;
}
function func() {
let filtered = [101, 98, 12, -1, 848].filter(isPositive);
console.log( "Positive elements in array: " + filtered);
}
func();
|
Output
Positive elements in array: 101,98,12,848
Method 5: Remove Array Elements with Index using Delete Operator
The delete operator returns a boolean value, true, if the element or property is removed from the array or object and false, if a function or variable is passed to remove.
Example:Â The below code implements the delete operator to remove elements from an array.
javascript
let array = [ "lodash" , "remove" , "delete" , "reset" ]
let deleted = delete array[2];
console.log( "Removed: " + deleted);
console.log( "Remaining elements: " + array);
|
Output
Removed: true
Remaining elements: lodash,remove,,reset
Method 6: Remove Array Elements using Clear and Reset Approach
Removing elements from an array using the manual clear and reset approach either by resetting the length of the array as 0 using the length property or by assigning the array to an empty array([]).
Example:Â This example explains how you can implement the clear and reset approach in JavaScript.
javascript
let array1 = [ "lodash" , "remove" , "delete" , "reset" ];
let array2 = [1, 2, 3, 4, 5];
console.log( "Array1 before elements removal: " , array1);
console.log( "Array2 before elements removal: " , array2);
array1 = [];
array2.length = 0;
console.log( "Array1 after elements removal: " , array1);
console.log( "Array2 after elements removal: " , array2);
|
Output
Array1 before elements removal: [ 'lodash', 'remove', 'delete', 'reset' ]
Array2 before elements removal: [ 1, 2, 3, 4, 5 ]
Array1 after elements removal: []
Array2 after elements removal: []
Method 7: Remove Element from Array using for() loop and new array
Here a simple for() will be run over the array and pushes all elements in the new array except the element that has to be removed.
Example: The below example uses the for loop and a new array to remove elements from an array.
Javascript
let removeElement = (array, n) => {
let newArray = [];
for (let i = 0; i < array.length; i++) {
if (array[i] !== n) {
newArray.push(array[i]);
}
}
return newArray;
};
let passed_in_array = [1, 2, 3, 4, 5];
let element_to_be_removed = 2;
let result = removeElement(passed_in_array, element_to_be_removed);
console.log( "Remaining elements: " + result);
|
Output
Remaining elements: 1,3,4,5
Use the lodash library to remove elements from a JavaScript array. To use lodash library you need to install it locally on your system.
Example:Â This code implements the _.remove() method of lodash to remove element from an array.
Javascript
const _ = require( 'lodash' );
let array = [101, 98, 12, -1, 848];
let evens= _.remove(array, function (n) {
return n % 2 == 0;
});
console.log( "Remaining odd elements: " + array);
console.log( "Removed even elements: " + evens);
|
Output:Â
Removed odd elements: 101, -1
Remaining even elements: 98, 12, 848
Last Updated :
06 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...