[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 ...
随机推荐
- 【解决方法】Unexpected namespace prefix “xmlns” found for tag Layout
问题描写叙述 出错代码例如以下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&quo ...
- hdu 4932
枚举差和差的1/2 #include <cstdio> #include <cstring> #include <algorithm> using namespac ...
- Android平台中的三种翻页效果机器实现原理
本文给开发者集中展现了Android平台中的三种翻页效果机器实现原理,希望能够对开发者有实际的帮助价值! 第一种翻页效果如下: 实现原理: 当前手指触摸点为a,则 a点坐标为(ax,ay), ...
- JS 保留2位小数 四舍五入(小数点后面不足2位,自动用0补齐)
function changeTwoDecimal_f(x) { var f_x = parseFloat(x); if (isNaN(f_x)) { alert('function:changeTw ...
- js常用数据转换&判断
数组转字符串 var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); //"0-1-2-3-4" 字符串转数组 ...
- query中prop()方法和attr()方法的区别
query1.6中新加了一个方法prop(),一直没用过它,官方解释只有一句话:获取在匹配的元素集中的第一个元素的属性值. 官方例举的例子感觉和attr()差不多,也不知道有什么区别,既然有了prop ...
- FarPoint.Win.Spread 常规操作
FarPoint.Win.Spread.FpSpread fSpread = new FarPoint.Win.Spread.FpSpread(); //设置 行数.列数 ...
- 1.5 Python基础知识 - while循环
在我们生活中有很多反复要做的事情,或者动作,我们称之为循环.在开发程序中也会有循环的事情要去做,就是需要反复的去执行某个代码,或者反复进行某种演算,直到达到某种条件的时候才会停止.在Python中我们 ...
- Java基础学习总结(31)——Java思维导图
- matlab无法使用
mathlab前一段时间使用不了,网上找到了一些出来方法. (1)修改本地时间,将本地时间改都较前的时间,就可以了, (2)使用网上的一个文件,将自己的文件替换掉就可以了 文件放在我的百度云盘里面ht ...