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. CentOS7--DNS处理模块DnsPython的简单使用

    初步了解: DnsPython是Python实现的一个DNS工具包,支持几乎所有的记录类型. 安装: # wget http://www.dnspython.org/kits/1.9.4/dnspyt ...

  2. C#中唯一标识符GUID的一些知识点

    概念 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) . GUID是一个通过特定算 ...

  3. EF中执行原生sql与使用Local获取本地数据

    使用DbSet的Local属性可以访问当前context中被追踪且没有被标记为删除的实体(内存中的数据) using (var context = new BloggingContext()) { / ...

  4. Code First 数据注释--InverseProperty 和 ForeignKey

    ForeignKey 按照约定在Post类中看到BlogId属性,会认为是Blog类的外键,但是在Blog类中并没有BlogId属性,解决方法是,在 Post 中创建一个导航属性,并使用 Foreig ...

  5. oracle 的服务器进程(PMON, SMON,CKPT,DBWn,LGWR,ARCn)

    来着TOM的<oracle 编程艺术 9i,10g,11g> PMON PMON,进程监视.PMON主要有3个用途: 1,在进程非正常中断后,做清理工作.例如:dedicated serv ...

  6. hive函数总结-日期函数

    获取当前UNIX时间戳函数: unix_timestamp语法: unix_timestamp() 返回值: bigint说明: 获得当前时区的UNIX时间戳举例: hive> select u ...

  7. silverlight visifire控件图表制作——silverlight 后台方法打印

    一.后台方法 1.添加引用:using System.Windows.Printing; 2.全局变量://定义图片和文本打印变量  PrintDocument printImage; 3.构造方法体 ...

  8. mysql 常用语法

    --创建数据库 CREATE DATABASE DB_NAME; --选中数据库 USE DB_NAME; --列出数据库列表 SHOW DATABASES; --删除数据库 DROP DATABAS ...

  9. redis数据结构与主要命令

    redis的数据类型有:string.hashes.lists.sets,sorted sets 1.string类型: set.get添加键值对获得键值对.如果多次赋值会覆盖掉原来的value se ...

  10. 优化PHP代码的40条建议(转载)

    [size=5][color=Red](译文)优化PHP代码的40条建议[/color][/size] 40 Tips for optimizing your php Code 原文地址:http:/ ...