In this lesson we'll take an array of objects and map it to a new array where each object is a subset of the original. We'll look at multiple ways to accomplish this, refactoring our code into a simple and easy to read function using Ramda's mappick and project functions.

Lets say we have an array of objects, we want to only pick the 'name' and 'price' props from each object:

const products = [
{name: 'Jeans', price:, category: 'clothes'},
{name: 'Hoodie', price:, category: 'clothes'},
{name: 'Jacket', price:, category: 'clothes'},
{name: 'Cards', price: , category: 'games'},
{name: 'iPhone', price: , category: 'electronics'},
{name: 'Sauce Pan', price: , category: 'housewares'}
] const result = products.map(p => ({name: p.name, price: p.price})) console.log(result);

It works but as we can image that if we need to pick 10 props or even more, then it would be a problem, the code would be hard to read.

We can improve this by using Ramda's pick method:

const result = products.map(p => R.pick(['name', 'price'], p))

Then we can utilize Ramda automaticlly curry function to improve the code:

const result = products.map(R.pick(['name', 'price']))

Then we can extract the bussniess logic into a sprate function to make it resuable:

const getNameAndPrice = R.map(R.pick(['name', 'price']));
const result = getNameAndPrice(products);

Since it is a common pattern that "map to each object in array and pick certain props will it",  we can use "R.project":

const getNameAndPrice = R.project(['name', 'price']);
const result = getNameAndPrice(products);

[Ramda] R.project -- Select a Subset of Properties from a Collection of Objects in Ramda的更多相关文章

  1. The R Project for Statistical Computing

    [Home] Download CRAN R Project About R Contributors What’s New? Mailing Lists Bug Tracking Conferenc ...

  2. [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 th ...

  3. [Ramda] Declaratively Map Data Transformations to Object Properties Using Ramda evolve

    We don't always control the data we need in our applications, and that means we often find ourselves ...

  4. Cannot detect Web Project version. Please specify version of Web Project through Maven project property <webVersion>. E.g.: <properties> <webVersion>3.0</webVersion> </properties>

    鼠标放在问题出会出现set web project version to 3.0和set web project version to 3.1两个选项 随便选一个版本就好了

  5. R 语言 select函数在org.Hs.eg.db上的运用

    首先org.Hs.eg.db是一个关于人类的 一,在R中导入包library(org.Hs.eg.db) http://www.bioconductor.org/packages/release/da ...

  6. In R, how to split/subset a data frame by factors in one column?

    按照某列的值拆分data.frame My data is like this (for example): ID Rate State 1 24 AL 2 35 MN 3 46 FL 4 34 AL ...

  7. Stream Processing 101: From SQL to Streaming SQL in 10 Minutes

    转自:https://wso2.com/library/articles/2018/02/stream-processing-101-from-sql-to-streaming-sql-in-ten- ...

  8. R语言 subset()函数用法

    subset() 函数: subset(dataset , subset , select ) dataset 是 要进行操作的数据集 subset 是对数据的某些字段进行操作 select 选取要显 ...

  9. 移除project,testsuite,testcase级别所有的custom properties

    // Remove all custom properties on Project level. If removed, custom properties cannnot be injected ...

随机推荐

  1. word2vec源代码解析之word2vec.c

    word2vec源代码解析之word2vec.c 近期研究了一下google的开源项目word2vector,http://code.google.com/p/word2vec/. 事实上这玩意算是神 ...

  2. Java BlockingQueue Example(如何使用阻塞队列实现生产者-消费者问题)

    Today we will look into Java BlockingQueue. java.util.concurrent.BlockingQueue is a java Queue that ...

  3. [React Intl] Format Numbers with Separators and Currency Symbols using react-intl FormattedNumber

    Using a react-intl FormattedNumber component, we'll pass a Number and a few additional props in orde ...

  4. 多类 SVM 的损失函数及其梯度计算

    CS231n Convolutional Neural Networks for Visual Recognition -- optimization 1. 多类 SVM 的损失函数(Multicla ...

  5. UVA 10603 - Fill BFS~

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...

  6. CSDN日报20170406 ——《代码非常烂,所以离职。》

    [程序人生]代码非常烂.所以离职? 作者:stormzhang 我在面试的时候一般会问这么一个问题:你为什么离职? 当中有不少同学会提到这么一个原因.现在的项目代码太烂了,前人留下了非常多坑,我实在忍 ...

  7. swift项目第八天:自定义转场动画以及设置titleView的状态

    如图效果: 一:Home控制器 /* 总结:1:设置登陆状态下的导航栏的左右按钮:1:在viewDidLoad里用三目运算根据从父类继承的islogin的登陆标识来判断用户是否登陆来显示不同的界面.未 ...

  8. Nginx+Tomcat+Memcached实现会话保持(MSM)

    会话保持的三种方式 Session sticky会话绑定:通过在前端调度器的配置中实现统一session发送至同一后发端服务器 Session cluster会话集群:通过配置Tomcat保持所有To ...

  9. [Node] Using dotenv to config env variables

    Install: npm install dotenv --save For example, we can store the sensitive information or env relate ...

  10. Swift 带有动画效果的TabBarItem

    额...貌似挺长时间没有总结新知识了,最近在看swift,之前swift刚出来的时候大体看了一遍,后来时间长了没看加之swift2.0做了比较大的调整,公司项目也不是用swift写的,也就没怎么看了, ...