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. 洛谷 P3131 [USACO16JAN]子共七Subsequences Summing to Sevens

    P3131 [USACO16JAN]子共七Subsequences Summing to Sevens 题目描述 Farmer John's NN cows are standing in a row ...

  2. 阿里云部署Docker(3)----指令学习

    通过上两节的学习http://blog.csdn.net/minimicall/article/details/40119177 和http://blog.csdn.net/minimicall/ar ...

  3. shader 3 rendering path

    渲染通道, rendering path. vertexlit, forward 和 Deferred lighting 旧有的非统一架构下: 分为顶点着色引擎和像素渲染通道 渲染通道是GPU负责给图 ...

  4. h5做app和原生app有什么区别?

    h5做app和原生app有什么区别? 一.总结 一句话总结: 二.h5做app和原生app有什么区别? 普通的HTML5技术与原生技术相比,有跨平台.动态.开放.直达二级内容页面等特点,但却在性能.工 ...

  5. Android 获取联系人手机号码、姓名、地址、公司、邮箱、生日

    public void testGetAllContact() throws Throwable { //获取联系人信息的Uri Uri uri = ContactsContract.Contacts ...

  6. VMWare中装Linux系统常见问题

    1.安装VMWare的时候,可能会提示vtx-m没开启 解决办法:重启笔记本电脑,按完开机键后,按住del或者F1或者F2,进入BIOS,在BIOS中找到intel-cietue开关,开启就 可以(如 ...

  7. C# 进制转换 在什么情况下使用16进制,字节数组,字符串

    C# 进制转换 Admin2013年9月18日 名人名言:从工作里爱了生命,就是通彻了生命最深的秘密.——纪伯伦 1.请问c#中如何将十进制数的字符串转化成十六进制数的字符串 //十进制转二进制Con ...

  8. POJ 题目2823 Sliding Window(RMQ,固定区间长度)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 46507   Accepted: 13442 ...

  9. Apache的.htaccess项目根文件夹伪静态设置规则

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  10. C#数据池

    //ThreadPool(线程池)是一个静态类,它没有定义任何的构造方法(),我们只能够使用它的静态方法,这是因为,这是因为ThreadPool是托管线程池(托管线程池http://msdn.micr ...