[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. Objects are converted into maps. Arrays are converted into lists. The fromJS() method can also take a reviver function for custom conversions.
const plainJSObject = {
title: "Go to grocery",
text: "I need milk and eggs",
completed: false,
category: {title: "House Duties", priority: 10}
}; const immutableTodo = Immutable.fromJS(plainJSObject); expect(Immutable.Map.isMap(immutableTodo)).to.be.true
We cat get value by getIn() method:
expect(immtableTodo.getIn(["category", "title"])).to.equal("House Duties");
Array to Immutable List:
check by isList():
const plainJSArray = [
"Go to grocery",
"Buy milk and eggs",
"Help kids with homework",
["Buy Lemons", "Make Lemonade"]
]; const immutableTodoList = Immutable.fromJS(plainJSArray);
expect(Immutable.List.isList(immutableTodoList)).to.be.true;
get value by getIn():
expect(immutableTodoList.getIn([3, 1])).to.equal("Make Lemonade")
Convert a plain array to Immutable Map:
const plainJSArray = [
"Go to grocery",
"Buy milk and eggs",
"Help kids with homework",
["Buy Lemons", "Make Lemonade"]
]; const immutableTodoList = Immutable.formJS(plainJSArray, (key, value)=>{ return value.toMap();
}); expect(immutableTodoList.getIn([3,1])).to.equal("Make Lemonade");
[Immutable.js] Using fromJS() to Convert Plain JavaScript Objects into 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 ...
- [Immutable.js] Differences between the Immutable.js Map() and List()
The Immutable.js Map() is analogous to a Javascript Object or Hash since it is comprised of key-valu ...
- Immutable.js 以及在 react+redux 项目中的实践
来自一位美团大牛的分享,相信可以帮助到你. 原文链接:https://juejin.im/post/5948985ea0bb9f006bed7472?utm_source=tuicool&ut ...
- React+Immutable.js的心路历程
这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...
- [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 – JavaScript 不可变数据集合
不可变数据是指一旦创建就不能被修改的数据,使得应用开发更简单,允许使用函数式编程技术,比如惰性评估.Immutable JS 提供一个惰性 Sequence,允许高效的队列方法链,类似 map 和 f ...
- [Javascript] Manage Application State with Immutable.js
Learn how Immutable.js data structures are different from native iterable Javascript data types and ...
- [Javascript] Modifying an Immutable.js Map()
We will now look at five methods that modify an Immutable.Map(). set update delete clear merge //set ...
- Redux进阶(Immutable.js)
更好的阅读体验 更好的阅度体验 Immutable.js Immutable的优势 1. 保证不可变(每次通过Immutable.js操作的对象都会返回一个新的对象) 2. 丰富的API 3. 性能好 ...
随机推荐
- [RxJS] Handling a Complete Stream with Reduce
When a stream has completed, you often need to evaluate everything that has happened while the strea ...
- IOS Layer的使用
CALayer(层)是屏幕上的一个矩形区域,在每一个UIView中都包含一个根CALayer,在UIView上的所有视觉效果都是在这个Layer上进行的. CALayer外形特征主要包括: 1.层的大 ...
- 搭建LAMP架构
1. 为什么下载源码包需要到官网上去下载?简单说就是为了安全,如果是非官方下载的源码包,有可能被别有用心的人动过手脚. 2. 64位机器是否可以安装32位rpm包?64位机器是否可以安装32位的mys ...
- puppet 4.4 System Requirements
puppet是linux下自动部署管理工具,有apply,agent/server两种模式,安装后默认为agent/server模式. apply模式下,每台机器均有自己的catalog文件,如果需要 ...
- java socket报文通信(二)报文的封装
昨天我们谈了怎么建立socket通信的服务端和客户端,今天我们就来谈一谈怎么封装报文. 什么是报文这里我就不在阐述了,不清楚的朋友可以自己去查资料.我们今天要谈的报文主要友以下几个部分组成: 3位同步 ...
- QTP描述性编程中往WebEdit控件输入文字问题
在网上查找到许多相关的描述性编程的案例,自己就想动手一试,于是在专家视图中输入如下代码: systemUtil.Run "http://www.baidu.com" wait(15 ...
- 阐述Lambada表达式
在C#2.0引入匿名方法之前,声明委托的唯一方法就是使用命名方法,C#2.0之后的C#3.0中开始引入了Lambda表达式取代了匿名方法. 匿名方法 要说Lambda必然离不开匿名方法,实际上,Lam ...
- zsh-替换掉黑白的控制台
官方地址:里面有详细的安装指南 http://ohmyz.sh/
- 基于nginx的rtmp的服务器(nginx-rtmp-module)
一,首先下载安装nginx需要依赖的库文件: 1.1,选定源码目录 选定目录 /usr/local/RTMP cd /usr/local/RTMP 1.2,安装PCRE库 cd /usr/local/ ...
- DOM 其他一些特性
cookie 允许javascript程序读写HTTP cookie 的特殊的属性 domain 允许当Web页面之间交互时,相同域名下相互信任的Web服务器之间协作放宽同源策略安全限制 (JavaS ...