[Ramda] allPass, propEq

const needs = [‘wifi‘, ‘shower‘, ‘laundry‘];

const homes = [{
  name: ‘Home 1‘,
  wifi: ‘y‘,
  shower: ‘y‘,
  laundry: ‘y‘,
  metro: ‘n‘,
  balcony: ‘y‘,
  fireplace: ‘n‘,
  pool: ‘y‘
}, {
  name: ‘Home 2‘,
  wifi: ‘n‘,
  shower: ‘y‘,
  laundry: ‘y‘,
  metro: ‘n‘,
  balcony: ‘n‘,
  fireplace: ‘n‘,
  pool: ‘n‘
}, {
  name: ‘Home 3‘,
  wifi: ‘y‘,
  shower: ‘y‘,
  laundry: ‘y‘,
  metro: ‘n‘,
  balcony: ‘y‘,
  fireplace: ‘y‘,
  pool: ‘n‘
}, {
  name: ‘Home 4‘,
  wifi: ‘y‘,
  shower: ‘y‘,
  laundry: ‘n‘,
  metro: ‘n‘,
  balcony: ‘n‘,
  fireplace: ‘n‘,
  pool: ‘n‘
}];

const propMatch = R.curry((toMatch, prop) => R.propEq(prop, toMatch));
const needsCheck = R.map(propMatch(‘y‘), needs);
const allNeedsMatch = R.allPass(needsCheck);
const res = R.filter(allNeedsMatch, homes);
console.log(res)
时间: 2024-08-25 22:32:47

[Ramda] allPass, propEq的相关文章

[Ramda] Change Object Properties with Ramda Lenses

In this lesson we'll learn the basics of using lenses in Ramda and see how they enable you to focus changes on specific properties of an object while keeping your data immutable. what 'R.lens' do is able to get or set prop value but keep the object i

[Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge

When doing comparisons inside of functions, you end of relying heavily on the argument passed into the function. Ramda's converge allows you to do comparisons in a Point-Free style allowing you more flexibility with composing and constructing functio

[Ramda] Filter, Reject and Partition

We'll learn how to get a subset of an array by specifying items to include with filter, or items to exclude using reject. We'll also look at how to get the results from both filter and reject, neatly separated with partition. // we don't need to requ

[React] Update State in React with Ramda's Evolve

In this lesson we'll take a stateful React component and look at how we can refactor our setState calls to use an updater function and then leverage Ramda's evolvefunction to make our updater function a reusable utility that isn't tied to the React A

[Ramda] Declaratively Map Predicates to Object Properties Using Ramda where

Sometimes you need to filter an array of objects or perform other conditional logic based on a combination of factors. Ramda's where function gives you a concise way to declaratively map individual predicates to object properties, that when combined,

[React] Update Component State in React With Ramda Lenses

In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. We'll create a lens to focus on the property we want to target and use over to apply the existing state value to a utility function and we'll get back

[Ramada] Build a Functional Pipeline with Ramda.js

We'll learn how to take advantage of Ramda's automatic function currying and data-last argument order to combine a series of pure functions into a left-to-right composition, or pipeline, with Ramda's pipe function. A simple example will take 'teams'

[Ramda] Sort, SortBy, SortWith in Ramda

The difference between sort, sortBy, sortWith is that: 1. sort: take function as args. 2. sortBy: take prop as args. 3. sortWith: take array of funcs as args. const R = require('ramda'); const {sort, sortBy, sortWith, descend, prop, ascend} = R; cons

[Ramda] Convert Object Methods into Composable Functions with Ramda

In this lesson, we'll look at how we can use Ramda's invoker and constructNfunctions to take methods of an object and turn them into reusable utility functions that are curried and accept their object as the last argument. We'll convert a dot-chained