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. kibana中信息分类查询显示的方法

    1.什么是kibana? kibana是ELK(elasticsearch+logstash+kibana)中的K,它是一个可灵活的分析和可视化平台,主要是显示数据以及根据这些数据绘出一些可视化图表, ...

  2. VA对于开发QT是神器,VA自动补全QT

    我怎么就忘了,VA也可以适用于VS下开发QT程序.其中QT的头文件自己增加,主要是: C:\Qt\4.8.6_2008\include 但还有一些特殊类不认识,所以还得继续增加: C:\Qt\4.8. ...

  3. Spring RootBeanDefinition,ChildBeanDefinition,GenericBeanDefinition

    转自:https://blog.csdn.net/joenqc/article/details/68942972 RootBeanDefinition,ChildBeanDefinition,Gene ...

  4. 1.1 Introduction中 Guarantees官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Guarantees Kafka的保证(Guarantees) At a high- ...

  5. 每日技术总结:fly.js,个位数前补零等

    01.FLY.JS 文档:https://wendux.github.io/dist/#/doc/flyio/readme 02.微信小程序组件——input属性之cursor-spacing 属性 ...

  6. 【Codeforces Round #447 (Div. 2) C】Marco and GCD Sequence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把gcd(a[1..n])放在输入的n个数之间. [代码] /* 1.Shoud it use long long ? 2.Have ...

  7. 观察者模式 VS 责任链模式

    为什么要把观察者模式和责任链模式放在一起对比呢?这两个模式没有太多的相似性呀,真没有嘛?有相似性,我们在观察者模式中也提到了触发链(也叫做观察者链)的问题,一个具体的角色既可以是观察者,也可以是被观察 ...

  8. 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)

    [链接]h在这里写链接 [题意] 给你一个n*n的矩阵. 其中每一列都有一个点. 任意两个点构成了矩形的两个对角点 ->即任意两个点确定了一个矩形. ->总共能确定n*(n-1)/2个矩形 ...

  9. 【习题 5-9 UVA - 1596】Bug Hunt

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] map模拟 map<string,int>记录每个数组的大小 map <pair<string, int&g ...

  10. js cookie创建读取删除函数封装

    js cookie创建读取删除函数封装 一.总结 都是为了方便操作,这样弄了很方便 1.创建cookie的函数封装的作用:方便设置过期时间expire,方便设置键和值 2.查询cookie的数据封装的 ...