[Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)
2017-02-28 20:13
501 查看
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
mapand
unnestand then refactoring to use
chain, AKA
flatMap. Finally, we'll add Ramda's
uniqfunction 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, 2)), 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, 2)); /* * 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" ] * */
相关文章推荐
- [JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)
- get an array of arrays with unique elements
- How to get the Values of Selected Row from a Gridview using ASP.NET
- Is it possible to get the difference from two dynamic arrays regardless of their order?
- [转]How to get return values and output values from a stored procedure with EF Core?
- 数据仓库Unable to get list of procedures from database meta-data: Table 'mysql.proc' doesn't exist
- Remove NULL values from PHP arrays with 1 line
- 【小熊刷题】Remove Nth Node From End of List <Leetcode 19, Java>
- Spark API 详解/大白话解释 之 map、mapPartitions、mapValues、mapWith、flatMap、flatMapWith、flatMapValues
- Spark API 详解/大白话解释 之 map、mapPartitions、mapValues、mapWith、flatMap、flatMapWith、flatMapValues
- map、mapPartitions、mapValues、mapWith、flatMap、flatMapWith、flatMapValues
- Write efficient code for extracting unique elements from a sorted list of array. e.g.
- Ruby Arrays: keep repeated values only from the end of an array of arrays
- <LeetCode><Easy> 19 Remove Nth Node From End of List
- 69.You need to generate a list of all customer last names with their credit limits from the CUSTOMER
- Use C# to get JSON Data from the Web and Map it to .NET Class => Made Easy! 转
- type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds int,java.lang.Object
- Passing a list of objects from view to controller via jquery ajax(Json(aspx) -> object(C#))
- The best way to get unique elements of a list in Python
- Get all the coordinates of listed banks from Google Maps with Python 3