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:

  1. //has()
  2. var map = Immutable.Map({a: '10'});
  3. console.log(map.has("a")); //true
  4.  
  5. //includes / contains
  6. var todo = {
  7. id: +new Date(),
  8. name: "name",
  9. content: "content"
  10. };
  11. var todo2 = {
  12. id: +new Date(),
  13. name: "name",
  14. content: "content"
  15. };
  16.  
  17. var todos = Immutable.Map();
  18. todos = todos.set(todo.id, todo);
  19. console.log(todos.contains(todo)); //true
  20. console.log(todos.contains(todo2)); //false

first(), getIn()

  1. // first(), getIn()
  2.  
  3. //Create tow Maps
  4. var todos = Immutable.Map();
  5. var todos2 = Immutable.Map();
  6.  
  7. //Create new Data
  8. var todo1 = {
  9. id: +new Date(),
  10. name: "name",
  11. content: "content"
  12. };
  13. var todo2 = {
  14. id: +new Date()+1000,
  15. name: "name",
  16. content: "content"
  17. };
  18. var todo3 = {
  19. id: +new Date()+3000,
  20. name: "name",
  21. content: "content"
  22. };
  23. var todo4 = {
  24. id: +new Date()+4000,
  25. name: "name",
  26. content: "content"
  27. };
  28.  
  29. //Add data to the map
  30. todos =todos.set(todo1.id, todo1);
  31. todos= todos.set(todo2.id, todo2);
  32. todos2=todos2.set(todo3.id, todo3);
  33. todos2=todos2.set(todo4.id, todo4);
  34.  
  35. //Wrap maps in another map
  36. var multipleTodoStates = Immutable.Map({
  37. "todo1": todos,
  38. "todo2": todos2
  39. });
  40.  
  41. //Get first todo's id in the first map
  42. const todoID = todos.first().id;
  43. console.log(todoID);
  44. //Try to find the first todo in deep map
  45. //"todo1" is the first level map
  46. //then downto the second level to find the id
  47. //If nohting return null
  48. var res = multipleTodoStates.getIn(["todo1", todoID], null);
  49. var res2 = multipleTodoStates.getIn(["todo2", todoID], null);
  50. console.log(res);
  51. console.log(res2);

find():

  1. //find()
  2. var todo = {
  3. id: +new Date(),
  4. name: "Wan",
  5. content: "Finish it"
  6. };
  7.  
  8. var todos = Immutable.Map();
  9. todos = todos.set(todo.id, todo);
  10.  
  11. var foundTodo = todos.find( (t)=>{
  12. return t.id === todo.id
  13. }, null, null) ;
  14.  
  15. 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. sublime text3-代码片段配置

    1.Tools->New Snippet-> <snippet>     <content><![CDATA[${1:public }function ${2 ...

  2. 下载安装sublime text3,打包sublime text3便携版,激活sublime text3,配置sublime text3的php环境

      下载安装sublime text3: http://www.sublimetext.com/3 安装就一直下一步   打包sublime text3便携版 : 参考http://segmentfa ...

  3. PHPUnit测试

    今天单元测试测到一个有点坑的小问题: public function testUpdataStatusFailForNegative() { // // Remove the following li ...

  4. mysql datestamp坑

    每次更改行数据,该行第一个datestamp如不赋值,会自动更新为当前时间.赋值还要注意用下new Date(time).updated_at要写在created_at前面...

  5. c++构造函数谁先执行的问题

    看到网上一哥们的帖子 http://blog.csdn.net/maray/article/details/7761709 东西不多就转发了 1 #include <iostream> u ...

  6. uboot环境变量初始化

    一.环境变量概述 1.环境变量的概念 可以理解为用户对软件的全局配置信息,这部分信息应该可以从永久性存储器上读取,能被查询,能被修改. 启动过程中,应该首先把环境变量读取到合适的内存区域,然后利用环境 ...

  7. React/React Native 的ES5 ES6写法对照表-b

    很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...

  8. 【Database】MySQL各版本的区别

    MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费, ...

  9. rm加转义很危险

    rm -r 想转义一个空格字符,转得不对 -r, -R, --recursive 递归删除目录及其内容 连续rm了n个不想rm的文件夹.%>_<% 想起来以前有人也因为rm的失误把整个wo ...

  10. python中并行遍历:zip和map-转

    http://blog.sina.com.cn/s/blog_70e50f090101lat2.html 1.并行遍历:zip和map 内置的zip函数可以让我们使用for循环来并行使用多个序列.在基 ...