The Array filter() is an inbuilt method, this method creates a new array with elements that follow or pass the given criteria and condition.
Few Examples have been implemented below for a better understanding of the concept
var newArray = arr.filter(callback(element[, index[, array]]) [, thisArg])
Parameter: This method accepts 2 parameters which was mentioned above and described below:
- Callback: The function is a predicate, to test each element of the array. Return true to keep the element, false otherwise. It accepts three arguments:
- element: The current element being processed in the array.
- index(Optional): The index of the current element being processed in the array.
- array(Optional): The array filter was called upon.
- thisArg(Optional): Value to use as this when executing the callback.
Example 1: The filter function filters all the numeric values in the array greater than 5
[ 6, 7, 8, 9, 10 ]
Example 2: The filter function filters all the words in the array which have length greater than 5
Example 3: The filter function filters all invalid id of users from the array.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.