Learn how to query an Immutable.Map() using get, getIn, has, includes, find, first and last. These are powerful operators that make finding data in an object graph pain free.

has, includes, contains:

//has()
var map = Immutable.Map({a: '10'});
console.log(map.has("a")); //true //includes / contains
var todo = {
id: +new Date(),
name: "name",
content: "content"
};
var todo2 = {
id: +new Date(),
name: "name",
content: "content"
}; var todos = Immutable.Map();
todos = todos.set(todo.id, todo);
console.log(todos.contains(todo)); //true
console.log(todos.contains(todo2)); //false

first(), getIn()

// first(), getIn()

//Create tow Maps
var todos = Immutable.Map();
var todos2 = Immutable.Map(); //Create new Data
var todo1 = {
id: +new Date(),
name: "name",
content: "content"
};
var todo2 = {
id: +new Date()+1000,
name: "name",
content: "content"
};
var todo3 = {
id: +new Date()+3000,
name: "name",
content: "content"
};
var todo4 = {
id: +new Date()+4000,
name: "name",
content: "content"
}; //Add data to the map
todos =todos.set(todo1.id, todo1);
todos= todos.set(todo2.id, todo2);
todos2=todos2.set(todo3.id, todo3);
todos2=todos2.set(todo4.id, todo4); //Wrap maps in another map
var multipleTodoStates = Immutable.Map({
"todo1": todos,
"todo2": todos2
}); //Get first todo's id in the first map
const todoID = todos.first().id;
console.log(todoID);
//Try to find the first todo in deep map
//"todo1" is the first level map
//then downto the second level to find the id
//If nohting return null
var res = multipleTodoStates.getIn(["todo1", todoID], null);
var res2 = multipleTodoStates.getIn(["todo2", todoID], null);
console.log(res);
console.log(res2);

find():

//find()
var todo = {
id: +new Date(),
name: "Wan",
content: "Finish it"
}; var todos = Immutable.Map();
todos = todos.set(todo.id, todo); var foundTodo = todos.find( (t)=>{
return t.id === todo.id
}, null, null) ; console.log(foundTodo.id);

[Javascript] Querying an Immutable.js Map()的更多相关文章

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

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

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

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

  4. [Immutable.js] Working with Subsets of an Immutable.js Map()

    Immutable.js offers methods to break immutable structures into subsets much like Array--for instance ...

  5. [Immutable,js] Iterating Over an Immutable.js Map()

    Immutable.js provides several methods to iterate over an Immutable.Map(). These also apply to the ot ...

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

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

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

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

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

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

随机推荐

  1. Python正则表达式2

  2. C语言笔记(二维数组与数值指针)

    一.关于二维数组和二维数组区别 (1)一维数组在内存中是连续分布存储的,同样,二维数组也是在内存连续存储的.所以从内存的角度来分析,一维数组和二维数组其实没有本质区别. (2) 二维数组可以使用一维数 ...

  3. 基于TCP的NAT子网穿透实验

    不得不说,在国内IP紧缺的现状下,NAT发挥了无比巨大的作用:它以把IP和端口重新分配的方式,满足了广大人民群众上网的强烈需求.但是对于个人服务器以及在内网中基于网络的嵌入式设备,却是个比较尴尬的事情 ...

  4. ASP.NET MVC轻教程 Step By Step 9——分页

    现在我们要把Index视图的留言信息进行分页显示. Step 1. 创建路由 我们希望以类似地址http://localhost:41583/Page1来表示第一页,Page2表示第二页,以此类推.在 ...

  5. CoreProfiler/NanoProfiler

    使用CoreProfiler/NanoProfiler实现跨平台&应用的整合性能调试 摘要 NanoProfiler是一个开源.NET性能调试类库,CoreProfiler是其.NET Cor ...

  6. WKWebView-b

    上一篇文章我们使用了JavaScriptCore框架重写了之前的示例,iOS8苹果偏爱HTML5,重构了UIWebVIew,给我们带来了WKWebView,使其性能.稳定性.功能大幅度提升,也更好的支 ...

  7. 000webhost找不到文件自定义错误

    1.新建一个名为.htaccess的文本文件:2.在文件中输入如下代码:ErrorDocument 404 /404.php3.保存文件,将.htaccess上传到域名的根目录,再验证,呵呵,成功了! ...

  8. Katu Puzzle

    poj3678:http://poj.org/problem?id=3678 题意:给你一些数,然后这些要么是0要么是1,然后回给出一些数之间的and,or,xor的值,问你是否存在一组解. 题解:2 ...

  9. vs2010编译curl为static库及测试

    1,编译curl为static库 用vs2010打开: curl-7.32.0\vs\vc6\vc6curl.dsw 选择LIB Release生成libcurl静态库: curl-7.32.0\vs ...

  10. 一个QT 3D转动控件

    其实说到底就是不停的截图,做出的幻觉.联想起360拖动图片,也是合并图片做出的效果,可见的对GUI来说图片是一切,是最根本的解决一切问题的办法,编程仅是辅助实现手段而已,我要记住这一点. .h文件 # ...