filter()
accepts two arguments.
filter()
runs the given function on every item
and returns an array of all items for which the
function returns true.
The function passed in receives three arguments.
For example, to return an array of all numbers greater than 2.
var numbers = [1,2,3,4,5,4,3,2,1];
var filterResult = numbers.filter(function(item, index, array){
return (item > 2);
});
console.log(filterResult); //[3,4,5,4,3]
filter()
does not change the values contained in the array.
The code above generates the following result.