Remove elements from a JavaScript Array
JavaScript array is a single variable that is used to store the elements or a group of values. You can add or remove elements from the array in any position. In this article, we will discuss different ways to remove elements from the array.
There are many methods that are used to remove elements from JavaScript array which are discussed below:
- JavaScript pop() function: This method is used to remove elements from the end of an array.
- JavaScript shift() function: This method is used to remove elements from the start of an array.
- JavaScript splice() function: This method is used to remove elements from the specific index of an array.
- JavaScript filter() function: This method is used to remove elements in a programmatic way.
Note: There are some other methods that are created by JavaScript inbuilt methods.
The below examples illustrate the methods to remove elements from a JavaScript array:
Remove Array elements by using the pop() method: 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.
Example 1:
javascript
<script> // JavaScript code to illustrate pop() function // to remove array elements function func() { var arr = [ "shift" , "splice" , "filter" , "pop" ]; // Popping the last element from the array var popped = arr.pop(); console.log( "Removed element: " + popped ); console.log( "Remaining elements: " + arr); } func(); </script> |
Output:
Removed element: pop Remaining elements: shift, splice, filter
Example 2:
javascript
<script> // Declare and initialize an array var array = [ "pop" , "splice" , "filter" , "shift" ] console.log( "Original array: " + array + "<br>" ) // Loop run while array length not zero while (array.length) { // Remove elements from array array.pop(); } console.log( "Array Length: " + array.length ) </script> |
Output:
Original array: pop, splice, filter, shift Array Length: 0
Remove Array elements by using the shift() method: This method is used to remove the first element of the array and reduce the size of the original array by 1.
Example:
javascript
<script> // JavaScript code to illustrate shift() method // to remove elements from array function func() { var arr = [ "shift" , "splice" , "filter" , "pop" ]; // Removing the first element from array var shifted = arr.shift(); console.log( "Removed element: " + shifted); console.log( "Remaining elements: " + arr); } func(); </script> |
Output:
Removed element: shift Remaining elements: splice, filter, pop
Remove Array elements by using the splice() method: This method is used to modify the contents of an array by removing the existing elements and/or by adding new elements. To remove elements by the splice() method you can specify the elements in different ways.
Example 1: Use the indexing of the splice method to remove elements from a JavaScript array.
javascript
<script> // JavaScript code to illustrate splice() function function func() { var arr = [ "shift" , "splice" , "filter" , "pop" ]; // Removing the specified element from the array var spliced = arr.splice(1, 1); console.log( "Removed element: " + spliced); console.log( "Remaining elements: " + arr); } func(); </script> |
Output:
Removed element: splice Remaining elements: shift, filter, pop
Example 2: Using the value of the splice method to remove elements from a JavaScript array.
javascript
<script> // JavaScript code to illustrate splice() function function func() { var arr = [ "shift" , "splice" , "filter" , "pop" ]; // Removing the specified element by value from the array for ( var i = 0; i < arr.length; i++) { if (arr[i] === "splice" ) { var spliced = arr.splice(i, 1); console.log( "Removed element: " + spliced); console.log( "Remaining elements: " + arr); } } } func(); </script> |
Output:
Removed element: splice Remaining elements: shift, filter, pop
Example 3: Using the splice method to remove each element from a JavaScript array.
javascript
<script> // Declare and initialize array var array = [ "pop" , "splice" , "filter" , "shift" ] console.log( "Original array: " + array) // Making the length of array to 0 by using splice method array.splice(0, array.length); console.log( "Empty array: " + array ) </script> |
Output:
Original array: pop, splice, filter, shift Empty array:
Remove Array elements by using the 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: Use the value of the filter method to remove elements from a JavaScript array.
javascript
<script> // JavaScript to illustrate filter() method function isPositive( value ) { return value > 0; } function func() { var filtered = [101, 98, 12, -1, 848].filter( isPositive ); console.log( "Positive elements in array: " + filtered); } func(); </script> |
Output:
Positive elements in array: 101, 98, 12, 848
Remove Array elements by using Remove Method: Creating a remove method using the filter method to remove elements from a JavaScript array. This method works in reverse order.
Example:
javascript
<script> // Declare and initialize an array var array = [ "lowdash" , "remove" , "delete" , "reset" ] // Using filter method to create a remove method function arrayRemove(arr, value) { return arr.filter( function (geeks){ return geeks != value; }); } var result = arrayRemove(array, "delete" ); console.log( "Remaining elements: " + result) </script> |
Output:
Remaining elements: lowdash, remove, reset
Remove Array elements by Delete Operator: Use the delete operator to remove elements from a JavaScript array.
Example:
javascript
<script> // Declare and initialize an array var array = [ "lowdash" , "remove" , "delete" , "reset" ] // Delete element at index 2 var deleted = delete array[2]; console.log( "Removed: " + deleted); console.log( "Remaining elements: " + array); </script> |
Output:
Removed: true Remaining elements: lowdash, remove,,reset
Remove Array elements by Clear and Reset Operator: Use the clear and reset operator to remove elements from a JavaScript array.
Example 1:
javascript
<script> // Declare and initialize an array var array = [ "lowdash" , "remove" , "delete" , "reset" ] // Sorting array in another array var arraygeeks = array // Delete each element of array array = [] console.log( "Empty array: " + array) console.log( "Original array: " + arraygeeks) </script> |
Output:
Empty array: Original array: lowdash, remove, delete, reset
Example 2:
javascript
<script> // Declare and initialize an array var array = [ "lowdash" , "remove" , "delete" , "reset" ] console.log( "Original array: " + array + "<br>" ) // Making the array length to 0 array.length = 0; console.log( "Empty array: " + array ) </script> |
Output:
Original array: lowdash, remove, delete, reset Empty array:
Remove Array elements using a simple for() loop and a new array: Here a simple for() will be run over the array and the except for the removed element, the remaining elements will be pushed into the new array which will be declared inside the function or a method.
Example:
Javascript
<script> 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); // This code is contributed by Aman Singla... </script> |
Output:
Remaining elements: 1,3,4,5
Remove Array elements by lowdash library: Use the lowdash library to remove elements from a JavaScript array. To use lowdash library you need to install it locally on your system.
Example:
html
< script type = "text/javascript" src = "//cdnjs.cloudflare.com/ajax/libs/lowdash.js/0.10.0/lowdash.min.js" > // Declare and initialize an array var array = [101, 98, 12, -1, 848]; var evens= _.remove(array, function(n) { return n % 2 == 0; }); console.log("odd elements: " + array + "< br >"); console.log("even elements: " + evens); </ script > |
Output:
odd elements: 101, -1 even elements: 98, 12, 848
Please Login to comment...