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.
- AngularJS | filter Filter
- PHP | Filter and Filter Constant
- PHP | Ds\Set filter() Function
- PHP | Ds\Map filter() Function
- CSS | filter Property
- AngularJS | currency Filter
- AngularJS | date Filter
- AngularJS | number Filter
- AngularJS | limitTo Filter
- AngularJS | lowercase Filter
- AngularJs | uppercase Filter
- PHP | Ds\Vector filter() Function
- PHP | Imagick filter() Function
- Underscore.js | _.filter() with Examples
- PHP | Ds\Sequence filter() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.