[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. 性能好 ...
随机推荐
- UIImagePickerController从拍照、图库、相册获取图片
iOS 获取图片有三种方法: 1. 直接调用摄像头拍照 2. 从相册中选择 3. 从图库中选择 UIImagePickerController 是系统提供的用来获取图片和视频的接口: 用UIImage ...
- Hacker(22)----解除系统中的密码
Win7系统中,用户可以设置BIOS密码以及系统登录密码,这些密码都有各自的用途.但对黑客而言,完全可以采用不同的方式绕过这些密码.下面介绍黑客如如何接触这些密码. 一.解除BIOS密码 BIOS密码 ...
- jquery之批量上传图片
//var btn; /** * * 获取当前时间 */ ==================================js=================================== ...
- [Asp.Net]状态管理(Session、Application、Cache、Cookie 、Viewstate、隐藏域 、查询字符串)
Session: 1. 客户在服务器上第一次打开Asp.Net页面时,会话就开始了.当客户在20分钟之内没有访问服务器,会话结束,销毁session.(当然也可以在Web.config中设置缓存时间 ...
- http 压缩
HTTP压缩是在Web服务器 和浏览器间传输压缩文本内容的方法.HTTP压缩采用通用的压缩算法如gzip等压缩HTML.JavaScript或 CSS文件.压缩的最大好处就是降低了网络传输的数据量,从 ...
- linux下mysql环境支持中文配置步骤
sql脚本执行前加上: CREATE DATABASE IF NOT EXISTS mydatabase DEFAULT CHARSET utf8 COLLATE UTF8_GENERAL_CI; u ...
- iOS_SN_深浅拷贝( 百度的)_转载
文章原地址:http://www.cnblogs.com/5ishare/p/4362459.html 深浅拷贝前提是:是实现NSCopying或者NSMutableCopying协议. 浅拷贝只是复 ...
- 记一次令人发狂的 bug Eclipse 开不开 tomcat 7.0
改项目,结果发现以前的项目也出了问题,就删除了系统用户下面workplace里的文件夹,结果,eclipse被清空,重新添加项目,发现一堆bug; 最让我崩溃的是,用tomcat 7.0跑项目,反复出 ...
- Win 7 通过事件管理器查看计算机开机关机时间
控制面板-管理工具-事件查看器 视图中开机来源:Kernel-General 事件ID:13 关机来源:Kernel-General 事件ID:12
- OpenGL ES 2.0 符点精度
片元着色器中使用符点相关类型的变量时与顶点着色器中有所不同,在顶点着色器中直接声明使用即可,而在片元着色器中必须指定精度. lowp 低 mediump 中 highp 高 指定整个着色器中符点相关类 ...