[Ramda] Filter an Array Based on Multiple Predicates with Ramda's allPass Function

In this lesson, we'll filter a list of objects based on multiple conditions and we'll use Ramda's allPass function to create a joint predicate from multiple, individual predicate functions.

 

const R = require('ramda');

const { allPass, propEq, lte, propSatisfies, filter } = R;

const cars = [
    {
        name: 'suv',
        doors: 4,
        mpg: 19
    },
    {
        name: 'sedan',
        doors: 4,
        mpg: 30
    },
    {
        name: 'hybrid',
        doors: 4,
        mpg: 37
    },
    {
        name: 'compact',
        doors: 2,
        mpg: 32
    }
];

const mpgLte30 = propSatisfies(lte(R.__, 30), 'mpg');
const fourDoors = propEq('doors', 4);
const preds = allPass([
                          mpgLte30,
                          fourDoors
                      ]);
const result = filter(preds)(cars);
console.log(result);

 

posted @ 2017-03-10 03:32  Zhentiw  阅读(469)  评论(0编辑  收藏  举报