Immutable.js iterables offer the reduce() method, a powerful and often misunderstood functional operator on which map(), filter(), groupBy(), etc. are built. The concept is simple: reduce transforms your iterable into something else, that's all. The name is misleading as you may or may not actually "reduce" anything. Let's replicate the groupBy() and filter() methods with reduce to illustrate how it works.

Assume you have a list to todos, each todo with a "completed" prop:

groupBy() like:

const todos = Immutable.List([
{
id: 1,
title: "Immutable.js",
completed: true
},
{
id: 2,
title: "RxJS",
completed: false
},
{
id: 3,
title: "ReactJS",
completed: false
}
]); const groupedTodos = todos.reduce( (acc, curr)=>{ let key = curr.completed ? "completed" : "Incompleted"; // Initial value is an Immutable Map object, so use get("completed") to get the Immutable.List(), then push the curr value into it
let list = acc.get(key).push(curr);
// Immutable return a new list from last push, so we need to set this list to the initial value
return acc.set(key, list); }, Immutable.Map({"completed": Immutable.List(), "Incompleted": Immutable.List()})); console.log(groupedTodos.get("Incompleted").get(1).title); //"ReactJS"

filter() like:

// Get all imcompleted todos
const filteredTodos = todos.reduce( (acc, curr)=>{ if(!curr.completed){
acc = acc.push(curr);
} return acc;
}, Immutable.List()); console.log(filteredTodos.get(1).title); // "ReactJS"

[Immutable.js] Transforming Immutable Data with Reduce的更多相关文章

  1. [Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types

    Immutable.js provides several conversion methods to migrate one structure to another. Each Immutable ...

  2. [Immutable,js] Immutable.Record() as data models

    The Immutable.js Record() allows you to model your immutable data much like you would model data wit ...

  3. [Immutable.js] Using fromJS() to Convert Plain JavaScript Objects into Immutable Data

    Immutable.js offers the fromJS() method to build immutable structures from objects and array. Object ...

  4. Redux进阶(Immutable.js)

    更好的阅读体验 更好的阅度体验 Immutable.js Immutable的优势 1. 保证不可变(每次通过Immutable.js操作的对象都会返回一个新的对象) 2. 丰富的API 3. 性能好 ...

  5. 深度浅出immutable.js

    这篇文章将讲述immutable.js的基本语法和用法. 1.fromJs()  Deeply converts plain JS objects and arrays to Immutable Ma ...

  6. 大话immutable.js

    为啥要用immutable.js呢.毫不夸张的说.有了immutable.js(当然也有其他实现库)..才能将react的性能发挥到极致!要是各位看官用过一段时间的react,而没有用immutabl ...

  7. React+Immutable.js的心路历程

    这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...

  8. [Javascript] Creating an Immutable Object Graph with Immutable.js Map()

    Learn how to create an Immutable.Map() through plain Javascript object construction and also via arr ...

  9. [Javascript] Manage Application State with Immutable.js

    Learn how Immutable.js data structures are different from native iterable Javascript data types and ...

随机推荐

  1. actionBar兼容2.1及以上版本的做法 .

    正在准备一个项目,需要尊重android design的同时还要做到很好的兼容低版本,于是就先从actionBar开始吧. 1,新建一个android工程startActionBar,minSdkVe ...

  2. Unity发送短信

    闲来无事,觉得用uinity来发送短信挺有意思的,所以自己差了点资料,看看能否实现,结果还真的可以!废话不多说,直接码! 1,新建一空工程,我们就简单的使用UGUI搭建一个丑陋的界面吧! 2,界面极其 ...

  3. MongoDB 和 mySql 的关系

    1. mysql 和 MongoDb MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL的数据库. ...

  4. ASE中的主要数据库

    Adaptive Server包括多种类型数据库: 必需数据库. “附加功能”数据库 .例子数据库 .应用数据库 1.必需数据库 master 数据库包含系统表,这些系统表中存储的数据被用来管理,有 ...

  5. 用SNMP协议实现系统信息监控--Windows Server 2008

    简单了解: SNMP简单网络管理协议,是一种属于应有层的协议,主要有三个部分组成,被管理部分.代理部分和网络管理系统. 被管理部分是一个网络节点,也称为网络单元.SNMP代理是被管理设备上的一个网络管 ...

  6. [转]iOS开发使用半透明模糊效果方法整理

    转自:http://www.molotang.com/articles/1921.html 虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包 ...

  7. nginx使用keepalived实现高可用

    环境: 主:linux-node1  110.0.0.137 备:linux-node2  110.0.0.138   VIP: 110.0.0.120   NGINX安装: # rpm -ivh h ...

  8. Js弹性漂浮广告代码

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  9. String功能测试

    package foxe; import java.io.IOException;import java.util.StringTokenizer; public class MainClass { ...

  10. About USB Data Link Cable API

    About USB Data Link Cable API The text on this webpage is licensed under the Creative Commons Attrib ...