博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Ramda] Pick and Omit Properties from Objects Using Ramda
阅读量:5142 次
发布时间:2019-06-13

本文共 1377 字,大约阅读时间需要 4 分钟。

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, 2)), x));const product = {    name: 'widget',    price: 10,    shippingWeight: 20,    shippingMethod: 'UPS'};// get one single prop from a large objectconst getByProp = pick(['name']); // { name: 'widget' }// different from R.propconst 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 conditionsconst notToPickByShippingProps = pickBy((val, key) => !key.includes('shipping')); // { name: 'widget', price: 10 }const result = notToPickByShippingProps(product);console.log(result);

 

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

你可能感兴趣的文章
Android中的菜单
查看>>
【最短路】Vijos P1046 观光旅游
查看>>
Android学习总结——开篇
查看>>
iOS 基础知识
查看>>
PHP 重新格式化var_dump/print_r打印的数组
查看>>
C++11:POD数据类型
查看>>
Delphi中Json格式读写
查看>>
请不要忘记带着梦想时常沐浴阳光
查看>>
交易与风险
查看>>
Hibernate: Could not find a getter for iUserId in class com.Hibernate.pojo.User异常
查看>>
windows环境下搭建RocketMQ
查看>>
CSS--基础
查看>>
基于OpenGL的渲染引擎
查看>>
Android HTTP GET 小文件下载
查看>>
ember.js:使用笔记4 数组数据的分组显示
查看>>
mvc-9测试和调试
查看>>
移植linux-2.6.32.2到qq2440
查看>>
转义字符(\)对JavaScript中JSON.parse的影响概述
查看>>
MySQL学习9 - 单表查询
查看>>
利用kubeadm部署k8s
查看>>