How to use for loop and push() Method In Javascript
This method will not mutate the original array. First, you have to create an empty() array and then loop over the new array and push only those elements that you want.
Example: This example shows the above-explained approach.
Javascript
let arr = [ 'gfg' , 'GFG' , 'g' , 'w3wiki' ]; const arrayWithoutGFG = []; for (let i = 0; i < arr.length; i++) { if (arr[i] !== 'GFG' ) { arrayWithoutGFG.push(arr[i]); } } // arr is same console.log(arr); console.log(arrayWithoutGFG); |
[ 'gfg', 'GFG', 'g', 'w3wiki' ] [ 'gfg', 'g', 'w3wiki' ]
Different ways to delete an item from an array using JavaScript
In Javascript, we do not have any array.remove() method for deleting the element. we will have an array and we need to delete a given item from that array and return the resulting array in the console.
These are the following methods for solving this problem:
Table of Content
- Using for loop and push() Method
- Using Pop() Method
- Using shift() Method
- Using splice() Method
- Using filter() Method
- Using delete Operator
- Using Lodash _.remove() Method
Note: There are some other methods that are created by JavaScript inbuilt methods.