How to use filter() Method In Javascript
This method returns the new array. Those array element that satisfies the condition of function is only passed on to the new array. This method does not change the original array.
Example: In this example, we will use the filter() method to delete an item from an array.
Javascript
const arr = [2, 7, 9, 15, 19]; function isPrime(n) { for (let i = 2; n > i; i++) { if (n % i === 0) { return false ; } } return n > 1; } console.log(arr.filter(isPrime)); |
Output
[ 2, 7, 19 ]
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.
Contact Us