[Ramda] R.project -- Select a Subset of Properties from a Collection of Objects in Ramda
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 map, pick 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的更多相关文章
- The R Project for Statistical Computing
[Home] Download CRAN R Project About R Contributors What’s New? Mailing Lists Bug Tracking Conferenc ...
- [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 ...
- [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 ...
- 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两个选项 随便选一个版本就好了
- R 语言 select函数在org.Hs.eg.db上的运用
首先org.Hs.eg.db是一个关于人类的 一,在R中导入包library(org.Hs.eg.db) http://www.bioconductor.org/packages/release/da ...
- 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 ...
- 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- ...
- R语言 subset()函数用法
subset() 函数: subset(dataset , subset , select ) dataset 是 要进行操作的数据集 subset 是对数据的某些字段进行操作 select 选取要显 ...
- 移除project,testsuite,testcase级别所有的custom properties
// Remove all custom properties on Project level. If removed, custom properties cannnot be injected ...
随机推荐
- 【AtCoder Regular Contest 082 A】Together
[链接]点击打开链接 [题意] 给你n个数字,每个位置上的数字可以+1,不变,或-1,每个位置只能操作一次. 操作完之后,让你选一个数字x,然后统计a[i]==x的个数count. 问你count的最 ...
- android开发设计辅助工具整理
1.Button设计工具button设计
- 一个开源.net混淆器——ConfuserEx (收藏)
一个开源.net混淆器——ConfuserEx http://yck1509.github.io/ConfuserEx/ 由于项目中要用到.net 混淆器,网上搜寻了很多款,比如Dotfuscator ...
- Haproxy 为 mysql 做负载均衡
.tar.gz cd haproxy- uname -r vim /etc/haproxy.cfg global #日志 log 127.0.0.1 local0 maxconn chroot /tm ...
- 系统学习java高并发系列一
转载请注明原创出处,谢谢! JAVA服务端或者后端需要大量的高并发计算,所以高并发在JAVA服务端或者后端编程中显的格外重要了. 首先需要有几个概念: 1.同步和异步 同步异步是来形容方法的一次调用的 ...
- UVA 11136 - Hoax or what (可以提交了,不会Submission error了)
看题传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- autohotkey 自动登录输入用户名密码 getElementsByTagName/getElementsByClassName/getElementById
针对button未设置id的.可以通过getElementsByTagName获取button的对象数组,再明确其在对象数组中的位置,如第4个button,通过[3]获取.再调用此对象的click() ...
- 基于mpvue的小程序项目搭建的步骤一
未标题-1.png mpvue 是美团开源的一套语法与vue.js一致的.快速开发小程序的前端框架,按官网说可以达到小程序与H5界面使用一套代码.使用此框架,开发者将得到完整的 Vue.js 开发体验 ...
- IOS使用AsyncSocket进行Socket通信
首先导入CFNetwork.framework框架 1.下载ASyncSocket库源码 2.把ASyncSocket库源码加入项目 3.在项目增加CFNetwork框架 使用AsyncSocket开 ...
- php对象和数组的相互转换(还是可以去找没有没php的高阶课程看看看)(要不别人分析一下重点要点,要不自己来,不然 效果真的不好)
php对象和数组的相互转换(还是可以去找没有没php的高阶课程看看看)(要不别人分析一下重点要点,要不自己来,不然 效果真的不好) 一.总结 都是自己实现的函数 算法: 1.先判断类型,gettype ...