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.

 Object to Immutable Map:
 
check by isMap():
    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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Immutable.js 以及在 react+redux 项目中的实践

    来自一位美团大牛的分享,相信可以帮助到你. 原文链接:https://juejin.im/post/5948985ea0bb9f006bed7472?utm_source=tuicool&ut ...

  4. React+Immutable.js的心路历程

    这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...

  5. [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 ...

  6. Immutable.js – JavaScript 不可变数据集合

    不可变数据是指一旦创建就不能被修改的数据,使得应用开发更简单,允许使用函数式编程技术,比如惰性评估.Immutable JS 提供一个惰性 Sequence,允许高效的队列方法链,类似 map 和 f ...

  7. [Javascript] Manage Application State with Immutable.js

    Learn how Immutable.js data structures are different from native iterable Javascript data types and ...

  8. [Javascript] Modifying an Immutable.js Map()

    We will now look at five methods that modify an Immutable.Map(). set update delete clear merge //set ...

  9. Redux进阶(Immutable.js)

    更好的阅读体验 更好的阅度体验 Immutable.js Immutable的优势 1. 保证不可变(每次通过Immutable.js操作的对象都会返回一个新的对象) 2. 丰富的API 3. 性能好 ...

随机推荐

  1. [Redux] Passing the Store Down with <Provider> from React Redux

    Previously, we wrote the Provider component by ourself: class Provider extends Component { getChildC ...

  2. Less 教程

    1. 关于 less sass 的预编译处理器 LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承, 运算, 函数. LESS 既可以在 客户端 上运行 (支持IE 6+, Webkit, ...

  3. python - socket模块1

    1.使用生活中的接打电话,解释socket通信流程  2.根据上图,写出socket通信的伪代码 2.1.server端伪代码 #买手机   #买手机卡 #开机 #等待电话 #收消息 #发消息 #挂电 ...

  4. NFinal 视图—模板

    创建模板 1.新建Header.ascx用户控件,此控件就是模板,修改内容如下: <%@ Control Language="C#" AutoEventWireup=&quo ...

  5. contenteditable 属性

    定义和用法 contenteditable 属性规定是否可编辑元素的内容. 语法 <element contenteditable="value"> 属性值 值 描述 ...

  6. 设置c#windows服务描述及允许服务与桌面交互的几种方法(转)

    方法一: 在ProjectInstaller.cs重写 install() ,Uninstall()方法 public override void Install(IDictionary stateS ...

  7. vi 快捷键【转】【weber整理必出精品】

    光标的移动 命令 光标移动 h或^h 向左移一个字符 j或^j或^n 向下移一行 k或^p 向上移一行 l或空格 向右移一个字符 G 移到文件的最后一行 nG 移到文件的第n行 w 移到下一个字的开头 ...

  8. 用C#代码控制水晶报表中的对象

    在C#代码中调用水晶报表的各个对象:字段对象:FieldObject obj=(FieldObject)oRpt.ReportDefinition.ReportObjects["FieldO ...

  9. 最简单轻便 的 sqlserver安装方式

    网上有很多版本高的sqlserver  下下来就超级费劲  ,所以特意的想了个办法 ,就省时间 最高效率的安装 需要两个软件 我们假定安装 sqlserver 2005 1.SQLEXPR32_CHS ...

  10. .net 4.0 面向对象编程漫谈基础篇读书笔记

    话说笔者接触.net 已有些年头,做过的项目也有不少,有几百万的,也有几十万的,有C/S的,也有B/S的.感觉几年下来,用过的框架不少,但是.net的精髓一直没有掌握.就像学武之人懂得各种招式,但内功 ...