[Immutable.js] Transforming Immutable Data with Reduce
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- Redux进阶(Immutable.js)
更好的阅读体验 更好的阅度体验 Immutable.js Immutable的优势 1. 保证不可变(每次通过Immutable.js操作的对象都会返回一个新的对象) 2. 丰富的API 3. 性能好 ...
- 深度浅出immutable.js
这篇文章将讲述immutable.js的基本语法和用法. 1.fromJs() Deeply converts plain JS objects and arrays to Immutable Ma ...
- 大话immutable.js
为啥要用immutable.js呢.毫不夸张的说.有了immutable.js(当然也有其他实现库)..才能将react的性能发挥到极致!要是各位看官用过一段时间的react,而没有用immutabl ...
- React+Immutable.js的心路历程
这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...
- [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 ...
- [Javascript] Manage Application State with Immutable.js
Learn how Immutable.js data structures are different from native iterable Javascript data types and ...
随机推荐
- 安装mysql时提示The host 'xxx' could not be looked up with resolveip的解决办法
1.首先用cat查看/etc/hosts文件,会显示以下内容: 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.loca ...
- 从反编译的角度去观察C#6.0
1. 自动属性初始化 (Initializers for auto-properties) 1.1 C#6.0 之前的写法 public class FirstExperience { private ...
- Oracle查找重复数据
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
- 提高jQuery执行效率
1. 使用最新版本的jQuery jQuery的版本更新很快,你应该总是使用最新的版本.因为新版本会改进性能,还有很多新功能. 下面就来看看,不同版本的jQuery性能差异有多大.这里是三条最常见的j ...
- Android Studio tips and tricks 翻译学习
Android Studio tips and tricks 翻译 这里是原文的链接. 正文: 如果你对Android Studio和IntelliJ不熟悉,本页提供了一些建议,让你可以从最常见的任务 ...
- 11 java 反射机制
Java反射机制的适用场景及其利与弊: http://blog.csdn.net/zolalad/article/details/29370565 http://my.oschina.net/u/10 ...
- 慕课linux学习笔记(八)常用命令(5)
解压缩命令 常用压缩格式 .zip .gz .bz2 .tar.gz .tar.bz2 zip [压缩文件名] [ 原文件 ] #压缩文件 -r [压缩文件名] [ 源目录] #压缩目录 -r [压缩 ...
- CSS3属性之一:border-radius
语法: border-radius : none | <length>{1,4} [ / <length>{1,4} ]? 相关属性: border-top-right-rad ...
- LoadRunner利用ODBC编写MySql脚本
最近做了几周的LoadRunner测试,有一些心得,记录下来,以便以后查找. LoadRunner测试数据库是模拟客户端去连接数据库服务器,因此,需要协议(或者说驱动的支持).LoadRunner本身 ...
- 【破解】破解ACDSEE15的方法
1.先从官方下载一个ACDSEE15简体中文版 2.下载注册机(点我下载) 3.修改注册表 修改注册表ACDSee 32位:HKEY_LOCAL_MACHINE\SOFTWARE\ACD System ...