[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 ...
随机推荐
- [Redux] Extracting Container Components (FilterLink)
Learn how to avoid the boilerplate of passing the props down the intermediate components by introduc ...
- telnet IP不通/sybase central工具无法连接到数据库
问题描述:客户端sybase central工具无法连接到数据库 服务端操作系统:RHEL5.8_x64,安装sybase-ASE15.7,端口号4112 IP:192.168.1.220 hos ...
- 未能加载文件或程序集“System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”
最近用vs2012发布程序,然后将更新后的程序文件部署到服务器上,由于服务器上本来有此系统,所以只更新了修改的文件 . 进行系统登录时提示:未能加载文件或程序集“System.Web.Extensio ...
- (转)js正则表达式之中文验证
今天做表单提交的输入框条件验证,验证是否包含中文:网上搜了一圈基于js正则表达式的验证基本不好用,而且大多都是出自一两篇原文的转帖!到底什么才是拿来主义呢.根据搜索结果,本文取精华,告诉大家一个好用的 ...
- Label 表达式绑定
Text='<%#"总金额为: "+Convert.ToString(Convert.ToDecimal(TextBox1.Text)*Convert.ToInt32(Tex ...
- Geodatabase - 打开数据库(工作空间)
//使用IName方式打开数据库(工作空间). public void GetWorkspace_IName(string workspacePath) { ESRI.ArcGIS.Geodataba ...
- Oracle函数function
--function /* 函数是有返回值.-只能有一个返回值. 语法 Create or replace func1(参数) Return varchar2 As Pl/sql块 Return 'J ...
- 编译XSIP过程中环境配置
昨天在编译XSip的过程中,有很多问题首先是出现了很多的error C1083. 然后到XSIP自己的文件夹中,也找不到对应的.h文件. 上网查阅后发现应该是缺少了对应的头文件的路径. 于是到可以 ...
- C#数组的指定位置复制函数
1. // 源数组 - 起始位置 -目的数组 - 起始位置 - 长度 System.Array.Copy(mcu_data, 2, read_mcu_data_whole, 0, mcu_data.L ...
- tomcat重启或关闭后,上传文件消失 .
tomcat重启或关闭后,上传文件消失的问题,是因为在断电前myeclipse是启动的,断电时造成myeclipse异常关闭,再重新启动myeclipse时会重新发布项目,把先前发布的项目给覆盖了,所 ...