How to use Array.filter method In Javascript
The Array.filter() method is used to create a new array with elements from the existing array which satisfies the condition.
Example: In this example, we will be using the filter() method to find all elements in an array except for the first element.
// Javascript program to find all
// element of array except first
// Function which takes an array as argument
const print = (arr) => {
// Returning resultant array
return arr.filter((x, y) => y != 0)
}
// Input array
const arr = [10, 20, 30, 40, 50]
// Printing the result
console.log(print(arr))
Output
[ 20, 30, 40, 50 ]
How to get all Elements Except First in JavaScript Array?
We will find all the elements in a given array except for the first one using JavaScript. We have a few methods to do this in JavaScript that are described below:
Table of Content
- Using for loop
- Using slice() Method
- Using Array.filter method
- JavaScript Array.reduce() Method
- Using shift() Method
- Using Array.from() Method
Contact Us