[Ramda] Pick and Omit Properties from Objects Using Ramda
Sometimes you just need a subset of an object. In this lesson, we'll cover how you can accomplish this using Ramda's pick and omit functions, as well as the pickAll and pickBy variants of pick.
const R = require('ramda');
const {pick, prop, pickBy, pickAll, props, omit, compose, not, curry} = R;
const log = curry((desc, x) => R.tap(() => console.log(desc, JSON.stringify(x, null, )), x));
const product = {
name: 'widget',
price: ,
shippingWeight: ,
shippingMethod: 'UPS'
};
// get one single prop from a large object
const getByProp = pick(['name']); // { name: 'widget' }
// different from R.prop
const getPropVal = prop('name'); // 'widget'
const getByProps = pickAll(['name', 'price']); // { name: 'widget', price: 10 }
const getPropsVals = props(['name', 'price']); // [ 'widget', 10 ]
const getByPickBy = pickBy((val, key) => {
// Only get prop if
// val: is number
// key contains 'shipping'
return Number(val) && key.includes('shipping');
}); // { shippingWeight: 20 }
const omitShippingProps = omit(['shippingWeight', 'shippingMethod']); // { name: 'widget', price: 10 }
// another way to omit props by conditions
const notToPickByShippingProps = pickBy((val, key) => !key.includes('shipping')); // { name: 'widget', price: 10 }
const result = notToPickByShippingProps(product);
console.log(result);
[Ramda] Pick and Omit Properties from Objects Using Ramda的更多相关文章
- [Ramda] R.project -- Select a Subset of Properties from a Collection of Objects in Ramda
In this lesson we'll take an array of objects and map it to a new array where each object is a subse ...
- [Ramda] Get Deeply Nested Properties Safely with Ramda's path and pathOr Functions
In this lesson we'll see how Ramda's path and pathOr functions can be used to safely access a deeply ...
- [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 ...
- [Ramda] Refactor to a Point Free Function with Ramda's useWith Function
Naming things is hard and arguments in generic utility functions are no exception. Making functions ...
- Appendix A. Common application properties
Appendix A. Common application properties Prev Part X. Appendices Next URl链接:https://docs.spring.i ...
- JavaScript- The Good Parts Chapter 3 Objects
Upon a homely object Love can wink.—William Shakespeare, The Two Gentlemen of Verona The simple type ...
- 【转】application.properties 常见配置
Various properties can be specified inside your application.properties/application.yml file or as co ...
- Introspection in Python How to spy on your Python objects Guide to Python introspection
Guide to Python introspection https://www.ibm.com/developerworks/library/l-pyint/ Guide to Python in ...
- JavaScript Objects in Detail
JavaScript’s core—most often used and most fundamental—data type is the Object data type. JavaScript ...
随机推荐
- 对象的序列化与反序列化---IO学习笔记(四)
对象的序列化,反序列化 对象的序列化: 就是将Object转换成byte序列 对象的反序列化: 将byte序列转换成Object 序列化流.反序列化流 序列化流(ObjectOutputStream) ...
- LinearLayout-layout_gravity 属性没有效果分析
今天在一个布局文件中,遇到了一个问题,先看代码 <LinearLayout android:layout_width="match_parent" android:layou ...
- drawable-图片绘制
首先看一下,下端代码 private Bitmap createSelectedChip(RecipientEntry contact, TextPaint paint) { int height = ...
- Android 监听软键盘点击回车及换行事件
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean ...
- springboot集成shiro 实现权限控制(转)
shiro apache shiro 是一个轻量级的身份验证与授权框架,与spring security 相比较,简单易用,灵活性高,springboot本身是提供了对security的支持,毕竟是自 ...
- Eclipse "Could not create java virtual machine"的问题解决
今天到了新的环境,需要重新搭建Android的开发环境,下载eclipse并安装了JDK1.6后,启动eclipse,发现出现了错误“Could not create Javavirtual mach ...
- C# 获取文件路径,读取项目中某程序集下文件
获取文件路径 ------------------------------------------------------------------------- winform获取文件路径: stri ...
- swift项目第七天:构建访客界面以及监听按钮点击
一:访客界面效果如图 二:xib封装访客视图的view 1:业务逻辑分析:1:由于用户未登录时要显示访客视图,要先进行判断用户是否登录,未登录则显示访客视图,登录则显示正常的登陆界面,由于要在四个子控 ...
- POJ 3628 Bookshelf 2 0-1背包
传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...
- swift学习第十四天:属性监听器
监听属性的改变 在OC中我们可以重写set方法来监听属性的改变 Swift中可以通过属性观察者来监听和响应属性值的变化 通常是监听存储属性和类属性的改变.(对于计算属性,我们不需要定义属性观察者,因为 ...