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. percona-toolkit的安装及简单介绍

    MySQL数据库是轻量级.开源数据库的佼佼者.其功能和管理,健壮性与Oracle相比还是有相当的差距.因此有非常多功能强大第三方的衍生产品,如percona-toolkit,XtraBackup等等. ...

  2. 【河南省多校脸萌第六场 A】巴什博弈?

    [链接]http://acm.nyist.me/JudgeOnline/problem.php?cid=1013&pid=5 [题意] 在这里写题意 [题解] 0..a-1 YES a..a+ ...

  3. 忍者无敌-实例解说Cocos2d-x瓦片地图

    实例比較简单,如图所看到的,地图上有一个忍者精灵,玩家点击他周围的上.下.左.右,他能够向这个方向行走. 当他遇到障碍物后是无法穿越的,障碍物是除了草地以为部分,包含了:树.山.河流等. 忍者实例地图 ...

  4. Tomcat源代码阅读#1:classloader初始化

    Bootstrap 通过Tomcat的启动脚本能够看到启动的入口是在Bootstrap,来看下Bootstrap的main方法, /** * Main method and entry point w ...

  5. vue使用(二)

    本节目标:           1.数据路径的三种方式          2.{{}}和v-html的区别 1.绑定图片的路径 方法一:直接写路径 <img src="http://p ...

  6. Spring拦截器和Servlet过滤器区别

    http://blog.csdn.net/chenleixing/article/details/44573495

  7. 51NOD——N 1107 斜率小于0的连线数量

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1107 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 ...

  8. WGS84与WGS84 Web Mercator

    1. WGS84与WGS84 Web Mercator 1.1 关于WGS1984投影坐标系 UTM (Universal Transverse Mercator)坐标系是由美国军方在1947提出的. ...

  9. Maven 使用Eclipse构建Maven的SpringMVC项目

    首先Eclipse需要安装Maven的插件,地址:http://m2eclipse.sonatype.org/sites/m2e. 用MyEclipse安装Maven插件,建出的Maven项目有些问题 ...

  10. 8.4 Android灯光系统_源码分析_电池灯

    电池灯的Java代码在batteryservice.java中 电池的状态电量等信息由驱动获得,但驱动不会主动做这些事情,因此肯定有个App调用驱动程序读取电池信息,称这个App为A应用. 还有个Ap ...