[Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)
In this lesson, we'll grab arrays of values from other arrays, resulting in a nested array. From there, we'll look at multiple ways to flatten the array structure using composition with map
and unnest
and then refactoring to use chain
, AKA flatMap
. Finally, we'll add Ramda's uniq
function to remove duplicate values.
const R = require('ramda'); const {map, chain, prop, pluck, compose, uniq, tap, curry} = R; const product = {
name: "Sample Data",
sizes: [
{
name: "L",
colors: [
{
name: "Red"
},
{
name: "Blue"
}
]
},
{
name: "M",
colors: [
{
name: "Green"
},
{
name: "Yellow"
}
]
},
{
name: "S",
colors: [
{
name: "Orange"
},
{
name: "Purple"
},
{
name: "Blue"
}
]
}
]
}; const log = curry((desc, x) => R.tap(() => console.log(desc, JSON.stringify(x, null, )), x)); // Target: to get unique array of color from product object const sizes = prop('sizes');
const getColors = chain(prop('colors')); // flatMap, get colors props from array of objects
const getColorNames = pluck('name'); // get name prop from array of objects
const res = compose(
uniq,
log("after name"),
getColorNames,
log("after colors"),
getColors,
log("after sizes"),
sizes
)(product); console.log(JSON.stringify(res, null, ));
/*
* after sizes [
{
"name": "L",
"colors": [
{
"name": "Red"
},
{
"name": "Blue"
}
]
},
{
"name": "M",
"colors": [
{
"name": "Green"
},
{
"name": "Yellow"
}
]
},
{
"name": "S",
"colors": [
{
"name": "Orange"
},
{
"name": "Purple"
},
{
"name": "Blue"
}
]
}
]
after colors [
{
"name": "Red"
},
{
"name": "Blue"
},
{
"name": "Green"
},
{
"name": "Yellow"
},
{
"name": "Orange"
},
{
"name": "Purple"
},
{
"name": "Blue"
}
]
after name [
"Red",
"Blue",
"Green",
"Yellow",
"Orange",
"Purple",
"Blue"
]
[
"Red",
"Blue",
"Green",
"Yellow",
"Orange",
"Purple"
]
* */
[Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)的更多相关文章
- SharePoint 2013 Content Deployment 报错 These columns don't currently have unique values
错误描述: These columns don't currently have unique values. Content deployment job 'job name' failed.The ...
- Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
在Tomcat的server.xml中配置两个context,出现其中一个不能正常启动,交换配置顺序,另一个又不能正常启动,即始终只有第二个配置能启动的情况.如果单独部署,都没有问题.报错大致内容如下 ...
- find unique values in an array
Problem: given an array that contains duplicates (except one value), find the one value that does no ...
- tomcat下部署了多个项目启动报错java web error:Choose unique values for the 'webAppRootKey' context-param in your web.xml files
应该是tomcat下部署了多个项目且都使用log4j. <!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root". ...
- Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决
大意是Log4jConfigListener在获取webapp.root值时,被后一context的值替换掉了,所以要在各个项目的web.xml中配置不同的webAppRootKey值,随即在其中一个 ...
- [Ramda] Convert a QueryString to an Object using Function Composition in Ramda
In this lesson we'll use a handful of Ramda's utility functions to take a queryString full of name/v ...
- Linq101-Set
using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Set { / ...
- coffeescript 1.8.0 documents
CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...
- how to use coffee script
TABLE OF CONTENTS TRY COFFEESCRIPT ANNOTATED SOURCE CoffeeScript is a little language that compiles ...
随机推荐
- Beginning iOS Programming
Beginning iOS Programming 2014年 published by Wrox
- 1.1 Introduction中 Consumers官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Consumers 消费者(Consumers) Consumers label t ...
- FreeMarker template error: The following has evaluated to null or missing
使用freemarker前端分页,报错: FreeMarker template error: The following has evaluated to null or missing 后端直接赋 ...
- JPA的配置文件persistence.xml参数详解
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0&q ...
- 硬件——STM32 , 录音,wav
详细的wav头文件解析,有例子:http://www.cnblogs.com/chulin/p/8918957.html 关于录音程序的编写: 我的思路是改写原子的程序,原子的程序需要借助VS1053 ...
- 鲁德http://www.testroad.org/topic/76
[最新的招聘信息]:1.联动优势科技有限公司招聘性能测试工程师 薪资20K左右 http://ask.testroad.org/article/4042.上海-交通银行总行-性能测试专家 ...
- GO语言学习(十一)Go 语言循环语句
Go 语言提供了以下几种类型循环处理语句: 循环类型 描述 for 循环 重复执行语句块 循环嵌套 在 for 循环中嵌套一个或多个 for 循环 语法 Go语言的For循环有3中形式,只有其中的一种 ...
- Leetcode之Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- js面向对象的选项卡
前言: 选项卡在项目中经常用到,也经常写,今天在github突然看到一个面向对象的写法,值得收藏和学习. 本文内容摘自github上的 helloforrestworld/javascriptLab ...
- mysql常见故障诊断
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010230971/article/details/80335578 作为故障预警,应该尽量把问题扼 ...