博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types
阅读量:5094 次
发布时间:2019-06-13

本文共 2032 字,大约阅读时间需要 6 分钟。

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

 

转载于:https://www.cnblogs.com/Answer1215/p/5206016.html

你可能感兴趣的文章
字符串处理
查看>>
ECharts(Enterprise Charts 商业产品图表库)初识
查看>>
LeetCode Factorial Trailing Zeroes (阶乘后缀零)
查看>>
hdu 5402 Travelling Salesman Problem (技巧,未写完)
查看>>
[AIR] 获取U盘,打开U盘
查看>>
HtmlUnitDriver 网页内容动态抓取
查看>>
django url 路由设置技巧
查看>>
三言两语说清“线性流程”
查看>>
(转)虚函数和纯虚函数区别
查看>>
ad logon hour
查看>>
获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName...
查看>>
证件照(1寸2寸)拍摄处理知识汇总
查看>>
Git入门简介
查看>>
eclipse里maven install时,报错提示jdk为无效的目标版本:1.7
查看>>
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
asp.net 获取IP地理位置的几个主要接口
查看>>
Python入门-函数
查看>>
[HDU5727]Necklace(二分图最大匹配,枚举)
查看>>
距离公式汇总以及Python实现
查看>>