ES 2015

Array methods - filter

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

Filter - example 2

Using an aray of bands (objects):
const bands = [
  {name: "Beastie Boys", category: "Rap", start: 1979, end: 2012},
  {name: "Nirvana", category: "Grunge", start: 1987, end: 1994},
  {name: "Run DMC", category: "Rap", start: 1981, end: 2002},
  {name: "Sound Garden", category: "Grunge", start: 1984, end: 1997},
  {name: "Queen", category: "Rock", start: 1970, end: 1991},
  {name: "The Beatles", category: "Pop", start: 1960, end: 1970}
];

Filter out the grunge bands:
const grungeBands = bands.filter(band => band.category === "Grunge");
RESULT GRUNGEBANDS