[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)}`

 

转载于:https://www.cnblogs.com/Answer1215/p/5944404.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值