cachaito
1/12/2019 - 11:51 AM

Programowanie funkcyjne

Zawróć uwagę na closure w funkcji isYoungerThan aby przechować wiek.

var arr = [{
  name: 'Agata',
  age: 23,
  active: false,
  gender: 'female'
},{
  name: 'Eryk',
  age: 31,
  active: true,
  gender: 'male'
},{
  name: 'Patrycja',
  age: 31,
  active: true,
  gender: 'female'
}]

const isActive = (item) => item.active;
const isFemale = (item) => item.gender === 'female';
const isYoungerThan = age => item => item.age > age; // <=== closure usage

const getActiveWomenBefore30 = (data) => {
  return data
    .filter(isActive)
    .filter(isFemale)
    .filter(isYoungerThan(30))
}