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

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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, cover the various facets of your conditions. In this lesson, we‘ll look at how this powerful function can be used for scenarios requiring a complex predicate function.

 

const products = [
  {name: Jeans, price:80, category: clothes, stock: 100},
  {name: Hoodie, price:50, category: clothes, stock: 20},
  {name: Sneakers, price:120, category: clothes, stock: 30},
  {name: Cards, price: 35, category: games, stock: 10},
  {name: iPhone, price: 649, category: electronics, stock: 5},
  {name: Sauce Pan, price: 100, category: housewares, stock: 200}
  ]

const predicate = R.where({
  category: R.complement(R.equals(clothes)), // category is not clothes
  stock: R.lt(R.__, 50), // less than 50
  price: R.gte(R.__, 100) // greater or equal than 100
})

const getResults = R.pipe(R.filter(predicate), R.pluck(name))
const result = getResults(products)
console.log(result)

document.getElementById(output).innerhtml = `${JSON.stringify(result)}`

 

以上是关于[Ramda] Declaratively Map Predicates to Object Properties Using Ramda where的主要内容,如果未能解决你的问题,请参考以下文章

如何使用Ramda将对象数组转换为列表

[Ramda] Basic Curry with Ramda

[Ramda] Sort, SortBy, SortWith in Ramda

[Ramda] Change Object Properties with Ramda Lenses

[Ramda] Rewrite if..else with Ramda ifElse

带有 FP 类型的 Ramda