[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.js class contains a prefixed "to" method like Map.toList(), Map.toSet(), etc. Converting these types sometimes results in a loss of data, as we will see when converting from Map to List.
Map to List:
it('should convert Map() to List()', () => { const map = Immutable.Map({
key1: 'First Item',
key2: 'Second Item'
}); const convertedList = map.toList(); expect(Immutable.List.isList(convertedList)).to.be.true; // Keys are discarded
expect(convertedList.first()).to.equal('First Item');
expect(convertedList.last()).to.equal('Second Item'); });
List to Map:
it('should convert List() to Map()', () => { const list = Immutable.List.of('First Item', 'Second Item'); const convertedMap = list.toMap(); // Converted keys ascend numerically
keys = convertedMap.keys();
expect(keys.next().value).to.equal(0);
expect(keys.next().value).to.equal(1); expect(Immutable.Map.isMap(convertedMap)).to.be.true; expect(convertedMap.first()).to.equal('First Item');
expect(convertedMap.last()).to.equal('Second Item'); });
Map to Javascript Array:
it('should convert Map() to Javascript Array', () => { const map = Immutable.Map({
key1: 'First Item',
key2: 'Second Item',
key3: {key4: 'Nested Item'}
}); const arr = map.toArray(); // Keys are discarded
expect(arr[0]).to.equal('First Item');
expect(arr[1]).to.equal('Second Item');
expect(arr[2].key4).to.equal('Nested Item'); });
Map to JSON:
it('should convert Map() to JSON', () => { const map = Immutable.Map({
key1: 'First Item',
key2: 'Second Item',
key3: {key4: 'Nested Item'}
}); const json = map.toJSON(); expect(json.key1).to.equal('First Item');
expect(json.key2).to.equal('Second Item');
expect(json.key3.key4).to.equal('Nested Item'); });
[Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types的更多相关文章
- [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 ...
- [Javascript] Modifying an Immutable.js Map()
We will now look at five methods that modify an Immutable.Map(). set update delete clear merge //set ...
- Numeral.js – 格式化和操作数字的 JavaScript 库
Numeral.js 是一个用于格式化和操作数字的 JavaScript 库.数字可以格式化为货币,百分比,时间,甚至是小数,千位,和缩写格式,功能十分强大.支持包括中文在内的17种语言. 您可能感兴 ...
- 【JavaScript】前端开发框架三剑客—AngularJS VS. Backone.js VS.Ember.js
摘要:透过对Github,StackOverflow,YouTube等社区进行数据收集后可知,AngularJS在各大主流社区中都是最受欢迎的,Backbone.js与Ember.js则不相伯仲.本文 ...
- Baffle.js – 用于实现文本模糊效果的 JavaScript 库
Baffle.js 是一个 JavaScript 库,设计用来模糊和揭开DOM元素的文本. 这些元素可以是一个 CSS 选择器的形式.一个节点列表或者一个单节点. 你也可以传递一个选择对象给插件. 在 ...
- JavaScript 之 动态加载JS代码或JS文件
2.动态加载JS文件 <script type="text/javascript"> function loadScript(url, callback) { var ...
- JavaScript模块化-require.js,r.js和打包发布
在JavaScript模块化和闭包和JavaScript-Module-Pattern-In-Depth这两篇文章中,提到了模块化的基本思想,但是在实际项目中模块化和项目人员的分工,组建化开发,打包发 ...
- Atitit.js模块化 atiImport 的新特性javascript import
Atitit.js模块化 atiImport 的新特性javascript import 1. 常见的js import规范amd ,cmd ,umd1 1.1. Require更多流行3 2. at ...
- Electron、Node.js、JavaScript、JQuery、Vue.js、Angular.js,layui,bootstrap
转载:https://blog.csdn.net/meplusplus/article/details/79033786 layui :是基于jquery库的封装开发. bootstrap:同样基于 ...
随机推荐
- LR翻页脚本并在每页实现业务操作
性能需求:在列表中删除后有记录,或对列表中的每条记录进行操作(如点击每条记录的“单号”进入订单详情页面,或在列表中对每条记录进行“启用”.“停止”操作) 举例:Vuser脚本模拟用户在订单列表中点击每 ...
- Android调试系列—使用android studio调试smali代码
1.工具介绍 使用工具 android killer:用于反编译apk包,得到smali代码 android studio:调试smali代码工具,或者使用idea,android studio就是在 ...
- 【转】深入理解篇UIScrollerView
转自:http://www.mamicode.com/info-detail-1144770.html 接下来,我整理一下自己的思路,深入理解 UIScrollView 基本点 : 1 . UIScr ...
- c#、sql数据库备份还原
1.在项目中添加SQLDmo dll文件引用(SQLDMO(SQL Distributed Management Objects,SQL分布式管理对象)) 2在相应页面加using SQLDMO引用 ...
- (转)Java 代码优化过程的实例介绍
简介: 通过笔者经历的一个项目实例,本文介绍了 Java 代码优化的过程,总结了优化 Java 程序的一些最佳实践,分析了进行优化的方法,并解释了性能提升的原因.从多个角度分析导致性能低的原因,并逐个 ...
- js数字精度丢失
http://www.cnblogs.com/snandy/p/4943138.html
- .NET 笔试分享
最近一直在面试,每次面试前也不怎么准备,虽说碰到的题大部分都很简单的,但是在现场答题的时候由于自己紧张脑子就空了,一些题答的不是很好,所以只有每次回来的时候才能好好想想怎么答: 题大部分还是挺简单的, ...
- Material Design说明
原文链接: Material Design 引言 我们挑战自我,为用户创造了一种视觉语言,综合了好设计的经典原则,革新以及科技的可能性.这就是material design.这份说明是一个动态的文档, ...
- 7 RandomAccessFile读取文件内容保存--简单例子(需要验证)
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; /** * 读取动态产生的文件内容 */ publ ...
- uva 10061 How many zero's and how many digits ?
How many zeros and how many digits? Input: standard input Output: standard output Given a decimal in ...