filter()
- WHAT - very similar to the forEach, takes a callback function as an arguement BUT checks the return value for matching callback criteria and returns a seperate array of data
- returns a new array with a subset of the original arrays elements, subset elements match the given callbacks functionality
- WHY - ] remove duplicate elements, ] find elements matching a particular condition, ...
- WHERE -
> The main difference between 'forEach' and 'filter' is that forEach just loops over the array and executes the callback but filter executes the callback and check its return value.
aka it RETURNS a seperate array with elements matching the given criteria in the callback function
KIM - the original 'cars' array still remains intact, we have created a second array, with a subset of elements from the original array that match the given criteria provided in our callback function
+ ref# 2 - the predicate function you pass to filter() should return either true, to allow that item in the list, or false to skip it.
- EXAMPLE - lets create an array of just the cars whose names start with the letter C
var cCars = cars.filter( function(element, index){
if (element.charAt(0) == 'C' ) return element ;
element.charAt(0) === 'C' ;
} )
// [ Corvette, Camaro, Charger ]
ID: 5743
NAME: JavaScript-Array-Methods
DESCRIPTION: JavaScript-Array-Methods - [] by Don Sagrott, founder @sospep.org - using the native javascript array methods of forEach, map, filter and reduce to manipulate your JavaScript arrays
AUTHOR: article.author/s
EDITOR: article.editor/s
PUBLISHER: article.publisher/s
STATUS: Write
PRIORITY: 0
OWNER ID: 75