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)的更多相关文章

  1. 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 ...

  2. Choose unique values for the 'webAppRootKey' context-param in your web.xml files!

    在Tomcat的server.xml中配置两个context,出现其中一个不能正常启动,交换配置顺序,另一个又不能正常启动,即始终只有第二个配置能启动的情况.如果单独部署,都没有问题.报错大致内容如下 ...

  3. find unique values in an array

    Problem: given an array that contains duplicates (except one value), find the one value that does no ...

  4. tomcat下部署了多个项目启动报错java web error:Choose unique values for the 'webAppRootKey' context-param in your web.xml files

    应该是tomcat下部署了多个项目且都使用log4j. <!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root". ...

  5. Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决

    大意是Log4jConfigListener在获取webapp.root值时,被后一context的值替换掉了,所以要在各个项目的web.xml中配置不同的webAppRootKey值,随即在其中一个 ...

  6. [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 ...

  7. Linq101-Set

    using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Set { / ...

  8. coffeescript 1.8.0 documents

    CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...

  9. how to use coffee script

    TABLE OF CONTENTS TRY COFFEESCRIPT ANNOTATED SOURCE CoffeeScript is a little language that compiles ...

随机推荐

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

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

  2. Docker---(3)Docker常用命令

    原文:Docker---(3)Docker常用命令 版权声明:欢迎转载,请标明出处,如有问题,欢迎指正!谢谢!微信:w1186355422 https://blog.csdn.net/weixin_3 ...

  3. Mongodb总结1-启动和Shell脚本

    2013年,还在秒针,当时听说了Mongodb,就学习了下,搞了下HelloWorld.主要是熟悉Mongodb的启动.命令行的Shell脚本.Java访问的CRUD. 今天,由于需要,再次回顾和进一 ...

  4. ActiveX控件开发 C#

    转自:http://hi.baidu.com/charlesx_kst/item/9c2f42e2920db3f42b09a4ff 前言: 这段时间因为工作的需要,研究了一下ActiveX控件.总结如 ...

  5. 配置PL/SQL Developer连接server数据库

    配置PL/SQL Developer连接server数据库 远程应用server上安装client客户端软件,可在oracle官网上下载. 举例: 环境 应用server操作系统 WIN 7 本地地址 ...

  6. Could not find action or result: There is no Action mapped for namespace [/] and action name [GetG

    Could not find action or result: /car/GetGpsDataAction  There is no Action mapped for namespace [/] ...

  7. Library Component Properties的表格如何填写

  8. 每天自动备份MySQL数据库的shell脚本

    经常备份数据库是一个好习惯,虽然数据库损坏或数据丢失的概率很低,但一旦发生这种事情,后悔是没用的.一般网站或应用的后台都有备份数据库的功能按钮,但需要去手工执行.我们需要一种安全的,每天自动备份的方法 ...

  9. C#自定义配置文件节的实现

    1.配置文件:(注意configSections必须放在最上面否则会报错) <?xml version="1.0" encoding="utf-8" ?& ...

  10. python课程:python3函数

    摘自廖雪峰的网站:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014316 ...