zoukankan      html  css  js  c++  java
  • [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, 2)), x));
    
    const product = {
        name: 'widget',
        price: 10,
        shippingWeight: 20,
        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);
  • 相关阅读:
    String.prototype.getParm
    IOS—通过ChildViewController实现view的切换
    objective-c IBOutletCollection介绍
    iOS方法类:CGAffineTransform的使用大概
    cocoaPods下载使用记录
    objective-c 中的关联介绍
    操作系统--文件管理
    操作系统--设备管理
    操作系统--存储管理的任务
    操作系统--并发进程死锁
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6481036.html
Copyright © 2011-2022 走看看