[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 ...
随机推荐
- 自定义seekbar中,thumb被覆盖掉一部分问题
- python-文件压缩和解压
import tarfile #压缩 tar = tarfile.open('your.tar','w') tar.add('ooo.xml',arcname='ooo.xml') tar.close ...
- CodeSmith使用总结--读取一个表试试
我感觉CodeSmith对于我的最大用途是不用我手动创建每个表的Model.BLL和DAL了,那些繁琐的工作真的让我很无语. CodeSmith要读取数据库中的表就要先连接数据库. 新建一个数据库连接 ...
- Html与css基础
1.html的定义 (1).html:超文本标记语言(HyperText Markup Language),它主要包括"头"(Head)和"主体"(Body)两 ...
- Sublime Text3 Package Control和Emmet插件安装方法
因为初学前端,所以今天安装了Sumblime Text 3,然后就停不下来去找Package Control的安装方法. 网络上我找到并尝试过的方法有两种,我使用的是用Python代码去安装并成安装成 ...
- vs2015升级后台mvc视图编辑器默认不是razor视图引擎问题
1.问题的原因 vs2013中创建的mvc4.0应用默认使用的razor2.0版本 在vs2015编辑器中默认使用的razor视图引擎是3.0版本 解决方案: 第一步:升级mvc应用的版本为mvc5. ...
- Js弹性漂浮广告代码
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- 表格table常见的边框设置和初步的立体效果
做网页时经常会遇到表格,常见的表格如下: <style type="text/css"> .tbtest0 { width:500px; height:200px; b ...
- C语言结构体的字节对齐
Test Code: #include <iostream> #include <cstring> using namespace std; struct A{ int a; ...
- 混入模式(max-in)实现继承
混入模式并不是一种复制完整的对象,而是从多个对象中复制出任意的成员并将这些成员组合成一个新的对象. 实现如下: function mix(){ var arg,prop,child = {}; for ...