[Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)
List of with gt from get VALUES unique
2023-09-14 09:00:52 时间
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, 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" ] * */
相关文章
- list转json字符串
- Java8中set转list
- 数组、IEnumerable、List、合分片或分块
- List<类型1>转成List<类型2>的LIst类型转换工具类
- ORA-01026: multiple buffers of size > 4000 in the bind list ORACLE 报错 故障修复 远程处理
- ORA-32038: number of WITH clause column names does not match number of elements in select list ORACLE 报错 故障修复 远程处理
- ORA-32483: duplicate name found in sort specification list for SEARCH clause of WITH clause ORACLE 报错 故障修复 远程处理
- ORA-38441: System could not derive the list of STORED and INDEXED attributes. ORACLE 报错 故障修复 远程处理
- ORA-39187: The transportable set is not self-contained, violation list is ORACLE 报错 故障修复 远程处理
- ORA-39284: Cannot alter the list values of a list subpartition: string ORACLE 报错 故障修复 远程处理
- ORA-02324: more than one column in the SELECT list of THE subquery ORACLE 报错 故障修复 远程处理
- ORA-13053: maximum number of geometric elements in argument list exceeded ORACLE 报错 故障修复 远程处理
- list java中List对象通用排序算法详解编程语言
- MySQL Status Mysqlx_stmt_list_notices 数据库状态作用意思及如何正确
- LIST-PROCESSING用法 ABAP任意时刻进行List输出_SAP详解编程语言
- C++ list,STL list(双向链表)详解
- Hibernate Query接口 list方法:返回查询结果的List集合
- 从Redis创建列表: 简单步骤(redis创建list)
- 长度查看Redis List长度:简单有效(redis查看list)
- 利用Redis List对象提升系统性能(redis list对象)
- 监测redis List动态稳定性突破极限(监听redis list)
- 以List的形式将数据插入Redis(把list插入redis)