The filter() method basically outputs all the element object that pass a specific test or satisfies a specific function. The return type of the filter() method is an array that consists of all the element(s)/object(s) satisfying the specified function.
var newArray = arr.filter(callback(object[, ind[, array]])[, Arg])
- Callback is a predicate, to test each object of the array. Returns True to keep the object, False otherwise. It takes in three arguments:
- Object: The current object being processed in the array.
- ind (Optional): Index of the current object being processed in the array.
- array (Optional): Array on which filter was called upon.
- Arg (Optional): Value to use(.this) when executing callback.
The above example returns all the positive elements in a given array.