In continuation of series of ECMAScript 5. This article describes filter feature of Array in ECMAScript 5. filter creates a new array and populates it with the array element for which callbackfn returns true. It loops through each element of the array in ascending order and triggers callbackfn for each element.
Array.filter()
Signature
array.filter(callbackfn [, thisArg])
callbackfn - It tests array elements. callbackfn should be defined as function (value[,index [,array]]).
thisArg- thisArg is optional. It is used to pass to callbackfn as this value.
Returns
filter returns a new array containing those elements which returns true to callbackfn.
Example
var arr = [87, 22, 54, 9, 39, 30, 31, 76, 39, 2];
arr.filter(function(x) { return !(x % 2); }) Output: [12, 44, 29, 20, 66, -8]
This ends the filter feature of array in ECMAScript 5.
|