In this article, we will be removing elements from an array in JavaScript. We can use a different method for removing elements.
These are the methods:
- Using pop() function
- Using shift() function
- Using splice() function
- Using filter() function
- Using Delete Operator
- UsingĀ _.remove() method
Approach 1: Using pop() function
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:
Javascript
// JavaScript code to illustrate pop() function // to remove array elements Ā
function func() { Ā Ā Ā Ā 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); } func(); |
Removed element: pop Remaining elements: shift,splice,filter
Approach 2: Using shift() function
This method is used to remove the first element of the array and reduce the size of the original array by 1.Ā
Example:
Javascript
// JavaScript code to illustrate shift() method // to remove elements from array function func() { Ā Ā Ā Ā 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); } func(); |
Removed element: shift Remaining elements: splice,filter,pop
Approach 3: Using splice() function
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)
Return Value: A new Array, containing the removed items (if any).Ā
Example: This example uses the splice() method to remove a particular element from an array.Ā
Javascript
let Arr = [ "C++ " , " Java " , Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā " Python " , " Go " , " Prolog" ]; Ā
function removeElement() { Ā Ā Ā Ā Arr.splice(2, 1); Ā Ā Ā Ā console.log(Arr); } Ā
removeElement(); |
[ 'C++ ', ' Java ', ' Go ', ' Prolog' ]
Approach 4: Using filter() function
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.Ā
Example:
Javascript
// JavaScript to illustrate filter() method 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(); |
Positive elements in array: 101,98,12,848
Approach 5: Using Delete Operator
Use the delete operator to remove elements from a JavaScript array.Ā
Example:
Javascript
// Declare and initialize an array let array = [ "lowdash" , "remove" , "delete" , "reset" ] Ā
// Delete element at index 2 let deleted = delete array[2]; Ā
console.log( "Removed: " + deleted); console.log( "Remaining elements: " + array); |
Removed: true Remaining elements: lowdash,remove,,reset
Approach 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.
Example:
Javascript
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
Supported Browsers: The browser supported by the Array splice() Method is listed below:
- Google Chrome
- Apple Safari
- Firefox
- Opera
- Edge