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. SSD: Single Shot MultiBox Detector

    By Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed, Cheng-Yang Fu, Alexande ...

  2. windows下配置wnmp

    最近尝试windows下配置nginx+php+mysql,在这里总结一下. 1.下载windows版本的nginx,官网​下载地址:http://nginx.org/en/download.htm, ...

  3. [Linux]关机和重启命令

     Linux中常用的关机和重新启动命令有shutdown.halt.reboot以及init,它们都可以达到关机和重新启动的目的,但是每个命令的内部工作过程是不同的,下面将逐一进行介绍. 1. shu ...

  4. php 获取客户端IP地址

    /** * 获取真实IP地址 */ /* 在PHP中getenv(参数)函数是一个用于获取环境变量的函数,根据提供不同的参数可以获取不同的环境变量, getenv("REMOTE_ADDR& ...

  5. python中的字典应用实例

    字典中的键使用时必须满足一下两个条件: 1.每个键只能对应一个项,也就是说,一键对应多个值时不允许的(列表.元组和其他字典的容器对象除外).当有键发生冲突时(即字典键重复赋值),取最后的赋值. > ...

  6. android调试bug集锦 onActivityResult立即返回,并且被CANCEL

    症状: 在使用startActivityForResult调用照相机或者选择图片的时候,总是onActivityResult立马返回,resultCode=0 CANCEL. startActivit ...

  7. 写了几天的博客-feel

    写博客 真的总结我自己的知识.长见识了.记录下自己遇到的东西,算是一个总结,有问题了,还可以回头看一下.很好 真的很好.

  8. 4d tensor

    偶然在一个ppt中看到了如下关于tensor的解释,清晰明白,所以post在这里,以备后续查看 根据这个理解: theano中的input(4d tensor):[mini-batch size, n ...

  9. node案例

    http://www.cnblogs.com/wewe/archive/2010/03/19/1685658.html http://www.laonan.net/blog/69/ http://cn ...

  10. 查看SQL Server数据库中各个表和视图的索引所占的空间大小

    ;with cte as ( (select t.name as TableName,i.name as IndexName, sum(row_count)as row_count, SUM (s.u ...