Filter - example 1
Using an aray of ages:const ages = [14, 16, 38, 7, 74, 28, 17, 44, 56];
Filter the array using the regular function notation.
const over21 = ages.filter(function(age) {
if(age >= 21){
return true;
}
});
const under21 = ages.filter(function(age) {
if(age < 21){
return true;
}
});
Or shorter, using the arrow functions:
const over21 = ages.filter(age => {
return age >= 21
});
const under21 = ages.filter(age => {
return age < 21
});
Or as a one liner:
const over21 = ages.filter(age => age >= 21);
const under21 = ages.filter(age => age < 21);
RESULT OVER 21
RESULT UNDER 21