[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)}`
时间: 2024-12-28 21:22:11

[Ramda] Declaratively Map Predicates to Object Properties Using Ramda where的相关文章

[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

[ES6] When should use Map instead of Object

Use Maps when keys are unknown until runtime: Map: let recentPosts = new Map(); createPost( newPost, (data)=>{ // Key unknown until runtime, so use Map recentPosts.set(data.author, data.message); }); Object: const POST_PRE_PAGE=15; // Keys are previo

JSON 字符串 转Map<String,List<Object o>>

前提:使用阿里巴巴的JSON包 String json = ""; //JSON字符串 Map map = JSON.parseObject(json, Map.class); //遍历 Map  使用迭代器 Iterator it = map.keySet().iterator(); while(it.hashNext()){ String values = map.get(it.next()).toString(); List<Object o> list = JSON

list中依据map&amp;lt;String,Object&amp;gt;的某个值排序

private void sort(List<Map<String, Object>> list) { Collections.sort(list, new Comparator<Map<String, Object>>(){ public int compare(Map<String, Object> o1, Map<String, Object> o2) { //依据map中某个字段,从小到大排序 return (Integer)

利用Map创建Copy Object

定义上下文行为,要继承Map package com.wjz.core; import java.io.Serializable; import java.util.Map; public interface CustomSubjectContext extends Map<String, Object> { void setSessionId(Serializable sessionId); Serializable getSessionId(); void setAuthenticated

Java核心_内省

Java核心_内省 查看java的api,发现有一个包java.bean咦,这个包是干什么的呢,原来,它是用来操作JavaBean对象的! 一.内省操作①JavaBean:一种特殊的Java类无参构造方法,每个属性提供getter和setter/Introspector/src/yuki/core/introspector/bean/Point.java package yuki.core.introspector.bean; public class Point { private int x

Apache-commons.BeanUtils浅析

BeanUtils:通过反射控制JavaBean属性的实用方法.     1.cloneBean(Object) Object          克隆一个bean是基于可用属性的get和set方法,即使这个bean类没有实现Cloneable接口方法的实现是有BeanUtilBean的newbean = bean.getClass().newInstance();对newbean的属性设值采用PropertyUtilsBean.copyProperties内部采用java的内省调用get/set

commons-beanutils使用介绍

commons-beanutils是Apache开源组织提供的用于操作JAVA BEAN的工具包.使用commons-beanutils,我们可以很方便的对bean对象的属性进行操作.今天为大家介绍一下该包的常用方法. 在介绍常用类之前,我们先来 编写一个用于测试的BEAN类: package com.gujin.entity; public class UserInfo { private String userId; private String userName; public UserI

jsp-servlet篇(三)

1.JSP原理分析及语法 查看 _jsp.java 源码路径: F:\apache-tomcat-9.0.14\work\Catalina\localhost\ROOT\org\apache\jsp JSP语法 注释 <!-- html注释-->    html注释,在网页源代码中可以看到 <%-- jsp注释--%>   在网页源代码中看不到. 输出数据到页面     <%= hello %> 3.JSP中的Java脚本片段 2,3 方式都在_japService()