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. Qt 图片浏览器 实现图片的放大缩小翻转等功能

    图片的功能 源码: wiget.h #ifndef WIDGET_H #define WIDGET_H #include <QPixmap> #include <QImage> ...

  2. xcode6.3 模版位置

    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templ ...

  3. 注意knn与kmeans的区别

    开始的时候,我居然弄混了. knn是分类方法,是通过新加入的节点最接近的N个节点的属性,来判定新的节点. kmeans是聚类方法,是先选择k个点作为k个簇的中点,然后分簇之后重新划定中心点,然后再分簇 ...

  4. 自己写的Android图表库XCL-Charts一些旧的样例

    话说有了灵感就要抓住,来了兴趣就要去研究它. 所以尽管近期非常忙.但我还是没有丢下Android图表实现的研究.最终如今我的图表库基类 基本上已经有点模样了.不在是小打小闹,而是能依传入參数非常灵活的 ...

  5. JIRA6.3.6 安装汉化破解指南

    JIRA6.3.6 安装汉化破解指南 近期试着安装了下JIRA,碰到了些问题.特记录下来,供后来者使用: 1.常规安装 1.1. 下载并安装jira 从官网下载atlassian-jira-6.3.6 ...

  6. JDBC 专题

    digest: getFetchSize()方法不是获得记录数,而是获得每次抓取的记录数,默认是0,也就是说不限制.可以用setFetchSize()来设置,而getFetchSize()是用来读出那 ...

  7. HDU 5372 线段树

    给出两种操作: 第i个0:在x位置插入一个长度为i的线段,并输出该线段共覆盖了多少之前增加的线段 1:删除第i次插入的线段 官方题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端点. ...

  8. 清除浮动.md

    清除浮动的三种方法 1 加空div层(.clear) 2 overflow属性设置(.clearo) 3 :after伪元素(.clearfix) <!DOCTYPE html> < ...

  9. Springboot + shiro 整合之Url拦截设置(转)

    shiro 整合到springboot 还是比较简单的,只需要新建一个spring-shiro.xml的配置文件: <span style="font-size:14px;" ...

  10. AE IColor.rgb 的计算

    原文 AE IColor.rgb 的计算方法 IColor的rgb属性 是通过对应 的红 绿 蓝 值计算出来的,那么AE的内部计算方法是什么呢? 其实就是一个256进制的BGR数.下面是转换算法: / ...