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'); });

最新文章

  1. MyBatis源码分析(5)——内置DataSource实现
  2. django url.py使用
  3. WebStorm 8.0.1 注册码
  4. CSS3 动画效果带来的bug
  5. 51 NOD 1384 全排列(STL 搜索)
  6. .net reflector激活
  7. GoAhead 嵌入式web
  8. event.preventDefault和恢复元素默认事件
  9. SQL读取系统时间的语法(转)
  10. ThinkPHP自动验证
  11. C# Socket 简易的图片传输
  12. C# 6 与 .NET Core 1.0 高级编程 - 41 ASP.NET MVC(下)
  13. Python新式类继承的C3算法
  14. Oracle实战笔记(第七天)之PL/SQL进阶
  15. 构建微服务开发环境3————Java应用的优秀管理工具Maven的下载安装及配置
  16. 阿里云被挖矿进程wnTKYg入侵的解决方法
  17. login shell 和 non-login shell 的区别
  18. exp迁移测试库10.2.0.5
  19. 《笨方法学Python》加分题32
  20. js 翻牌活动效果

热门文章

  1. [RxJS] Handling a Complete Stream with Reduce
  2. Samba-ADS/WINBIND
  3. Linux查看网络即时网速
  4. (转)通过在 Page 指令或 配置节中设置 validateRequest=false 可以禁用请求验证
  5. linux 虚拟机centos64位_6.5+VM10 主机是固定IP局域网设置代理上网,虚机设置固定ip 图文详细步骤
  6. exc_bad_access(code=1, address=0x789870)野指针错误
  7. 关于发布DIPS的MVC项目的IIS 7.0环境配置的方法
  8. 吸血鬼数字算法参考 -- javascript版本
  9. Python Set集合,函数,深入拷贝,浅入拷贝,文件处理
  10. The Time in Words