JavaScript - Arrays - filter


// JavaScript - Array.prototype.filter():
The filter() method creates a new array with all elements that pass the test 
implemented by the provided function.

var new_array = arr.filter(callback[, thisArg])

function isBigEnough(value) {
  return value >= 10;
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]

callback: Function is a predicate, to test each element of the array. Return 
true to keep the element, false otherwise.  This function take three arguments:
1. The current element being processed in the array.
2. The index of the current element being processed in the array.
3. The array filter was called upon.

thisArg: Optional. Value to use as this when executing callback.

The filter method returns a new array with the elements that pass the test.

filter() calls a provided callback function once for each element in an array, 
and constructs a new array of all the values for which callback returns a value 
that coerces to true. callback is invoked only for indexes of the array which 
have assigned values; it is not invoked for indexes which have been deleted or 
which have never been assigned values. Array elements which do not pass the 
callback test are simply skipped, and are not included in the new array.

The filter() method does not mutate the array on which it is called.

The range of elements processed by filter() is set before the first invocation 
of callback. Elements which are appended to the array after the call to filter() 
begins will not be visited by callback. If existing elements of the array are 
changed, or deleted, their value as passed to callback will be the value at the 
time filter() visits them; elements that are deleted are not visited.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License