[Javascript] Querying an Immutable.js Map()
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()的更多相关文章
- [Javascript] Modifying an Immutable.js Map()
We will now look at five methods that modify an Immutable.Map(). set update delete clear merge //set ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- Immutable.js – JavaScript 不可变数据集合
不可变数据是指一旦创建就不能被修改的数据,使得应用开发更简单,允许使用函数式编程技术,比如惰性评估.Immutable JS 提供一个惰性 Sequence,允许高效的队列方法链,类似 map 和 f ...
- [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 ...
- [Javascript] Manage Application State with Immutable.js
Learn how Immutable.js data structures are different from native iterable Javascript data types and ...
- [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 ...
随机推荐
- sublime text3-代码片段配置
1.Tools->New Snippet-> <snippet> <content><![CDATA[${1:public }function ${2 ...
- 下载安装sublime text3,打包sublime text3便携版,激活sublime text3,配置sublime text3的php环境
下载安装sublime text3: http://www.sublimetext.com/3 安装就一直下一步 打包sublime text3便携版 : 参考http://segmentfa ...
- PHPUnit测试
今天单元测试测到一个有点坑的小问题: public function testUpdataStatusFailForNegative() { // // Remove the following li ...
- mysql datestamp坑
每次更改行数据,该行第一个datestamp如不赋值,会自动更新为当前时间.赋值还要注意用下new Date(time).updated_at要写在created_at前面...
- c++构造函数谁先执行的问题
看到网上一哥们的帖子 http://blog.csdn.net/maray/article/details/7761709 东西不多就转发了 1 #include <iostream> u ...
- uboot环境变量初始化
一.环境变量概述 1.环境变量的概念 可以理解为用户对软件的全局配置信息,这部分信息应该可以从永久性存储器上读取,能被查询,能被修改. 启动过程中,应该首先把环境变量读取到合适的内存区域,然后利用环境 ...
- React/React Native 的ES5 ES6写法对照表-b
很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...
- 【Database】MySQL各版本的区别
MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费, ...
- rm加转义很危险
rm -r 想转义一个空格字符,转得不对 -r, -R, --recursive 递归删除目录及其内容 连续rm了n个不想rm的文件夹.%>_<% 想起来以前有人也因为rm的失误把整个wo ...
- python中并行遍历:zip和map-转
http://blog.sina.com.cn/s/blog_70e50f090101lat2.html 1.并行遍历:zip和map 内置的zip函数可以让我们使用for循环来并行使用多个序列.在基 ...