[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 ...
随机推荐
- Ubuntu配置sublime text 3的c编译环境
新建编译系统 c语言 选择tool –> Build System –> New Build System 然后输入下面代码 { "shell_cmd": " ...
- libcurl 通过http协议下载文件并显示下载进度
vc6 测试工程下载地址:http://download.csdn.net/detail/mtour/8068053 代码如下: size_t my_write_func(void *ptr, siz ...
- hibernate 的映射文件快速生成:使用CodeSmith快速生成映射文件和映射类
一 CodeSmith简介 本文以表自动生成NHibernate的映射文件和映射类的实例来说明一下本软件的使用方法. CodeSmith是一种基于模板的代码生成工具,其使用类似于ASP.NET的语法来 ...
- BZOJ——3343: 教主的魔法 || 洛谷—— P2801 教主的魔法
http://www.lydsy.com/JudgeOnline/problem.php?id=3343 || https://www.luogu.org/problem/show?pid=280 ...
- COGS——C 908. 校园网 || 洛谷——P 2746 [USACO5.3]校园网Network of Schools
http://www.cogs.pro/cogs/problem/problem.php?pid=908 || https://www.luogu.org/problem/show?pid=27 ...
- [Android 性能优化系列]内存之提升篇--应用应该怎样管理内存
大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 原文地 ...
- synchronized和AtomicInteger解决并发问题的性能比较
AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字.而volatile ...
- JavaScript中双叹号“!!”作用
1.JavaScript的逻辑非(!)操作符的作用 (逻辑非) 如果操作数能够转换为true则返回false:否则返回true. 2.!!的作用 !!一般用来将后面的表达式强制转换为布尔类型的数据(b ...
- vue项目对axios的全局配置
标准的vue-cli项目结构(httpConfig文件夹自己建的): api.js: //const apiUrl = 'http://test';//测试域名,自己改成自己的 const apiUr ...
- DBeaver笔记-快捷键篇
公司使用的是PostgreSQL数据库,可以使用pgAdmin或者DBeaver进行连接该数据库.个人更喜欢用DBeaver,因为其界面更加美观,操作也相对简单.对于习惯了eclipse的开发者来说, ...