. Advertisement .
..3..
. Advertisement .
..4..
This tutorial introduces six methods to remove elements from a array in javascript.
How To Remove Elements From A Array in Javascript
Method 1: Use Pop() Method
This method removes the array’s last element and then returns the removed one. It can decrease the length by 1.
Example:
Input:
<!DOCTYPE html>
<script>
// JavaScript code to illustrate pop() function to remove array elements
function func() {
var array1 = ["Frankfurt", "Amsterdam", "Moscow", "Singapore"];
// Popping the last element from the array
var popped = array1.pop();
document.write("The removed element: " + popped + "<br>");
document.write("The remaining array:: " + array1);
}
func();
</script>
Output:
The removed element: Singapore
The remaining array:: Frankfurt,Amsterdam,Moscow
Method 2: Use shift() Method
You can use this method to remove the first element and reduce the original array’s size by 1.
<script>
// JavaScript code to illustrate shift() method to remove elements from array
function func() {
var array2 = ["Frankfurt", "Amsterdam", "Moscow", "Singapore"];
// Removing the first element from array
var shifted = array2.shift();
document.write("The removed element: " + shifted + "<br>");
document.write("The remaining array: " + array2);
}
func();
</script>
Output:
The removed element: Frankfurt
The remaining array: Amsterdam,Moscow,Singapore
Method 3: Use splice() method
If you want to modify the contents and value of an array, this method is the most suitable. It will both add a new element and remove an existing one.
Input:
<script>
// JavaScript code to illustrate splice() function
function func() {
var array3 = ["Frankfurt", "Amsterdam", "Moscow", "Singapore"];
// Removing the specified element from the array
var spliced = array3.splice(2, 2);
document.write("The removed element: " + spliced + "<br>");
document.write("The remaining array: " + array3);
}
func();
</script>
Output:
Removed element: Moscow,Singapore
Remaining elements: Frankfurt,Amsterdam
Method 4: Use filter() Method
The filter() method can develop a new array from a given one, which can satisfy a condition by an argument function. It can remove items from the array by value as well.
Input:
<script>
// JavaScript to illustrate filter() method
function isPositive( value ) {
return value > 0;
}
function func() {
var array4 = [12, 29, -22, 28, 19, -17].filter( isPositive );
document.write("The positive numbers in the array are: " + array4);
}
func();
</script>
Output:
The positive numbers in the array are: 12,29,28,19
Method 5: Use Delete Operator
Input:
<script>
// Declare and initialize an array
var array5 = ["Falcon", "Penguin", "Flamingo", "Swan", "Eagle"]
// Delete element at index 3
var removed_element = delete array5[3];
document.write("Removed: " + removed_element + "<br>");
document.write("The remaining array: " + array5);
</script>
Output:
The removed element: true
The remaining array: Falcon,Penguin,Flamingo,Eagle
Method 6: Use Lodash Library
To use this method, you will have to install it in your system and type in the code as follows:
Input:
<script type="text/javascript" src=
"//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js">
// Declare and initialize an array
var array6 = [15, 29, 22, 28, 19, 17];
var evens_number= _.remove(array6, function(x) {
return x % 2 == 0;
});
console.log("odd elements: " + array6 + "<br>");
console.log("even elements: " + evens_number);
</script>
Output:
The odd number: 15, 29, 19, 17
The even number: 22, 28
Conclusion
There are six methods to remove elements from a array in javascript. Depending on your needs, you should choose a suitable method.
Leave a comment