[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 ...
随机推荐
- ZendFramework使用中常见问题
MVC 代码书写:控制器代码书写:<?phpclass IndexController extends Zend_Controller_Action{ function init() { $th ...
- 学学Whatsapp,如何让自己挣160亿美金,然后退休?开发个J2ME应用。
facebook用160亿美元收购了Whatsapp,要知道这是facebook市值1600亿美元的十分之一,而Whatsapp是一个只有50名员工的小公司,这个价格让硅谷各种科技公司大佬跌破镜框.其 ...
- linux源码Makefile的详细分析
目录 一.概述 1.本文的意义 2.Linux内核Makefile文件组成 二.Linux内核Makefile的“make解析”过程 1 顶层Makefile阶段 1.从总目标uImage说起 2.v ...
- LightOj_1079 Just another Robbery
题目链接 题意: 抢银行(这个背景最爱了), 有n家银行, 每家银行抢劫被抓的概率是p[i],你认为当你被抓的概率低于P的时候是安全的. 问, 你最多能抢劫到多少money. 思路: 抽象成背包问题, ...
- Hibernate中的一对一关系详解(1)
A:先讲讲一对一的关系(欲知其他关系,请看下篇) a:主键关联的一对一关系 一对一关系一般用主键关联,也就是说用主键值来维护两者的关系,一个表的主键存放另一个表的主键值.例如在员工与帐号中,我们取员工 ...
- ios7新特性3-Map Kit新特性
Map Kit 框架 (MapKit.framework) 包含了大量的改进以及为基于地图的程序提供了新特性.利用地图显示位置信息的应用现在可以使用Maps这个程序用到的3D地图,包括控制程序控制视线 ...
- Fail2ban用来作DDOS防守工具,不知够不够份量
http://www.serversyntax.com/2012/12/how-to-secure-centos-server-ssh-fail2ban-ddos-deflate.html http: ...
- [LeetCode#265] Paint House II
Problem: There are a row of n houses, each house can be painted with one of the k colors. The cost o ...
- SQL Server 中各个系统表的作用
来源:http://www.hoky.org/blog/ sysaltfiles 主数据库 保存数据库的文件syscharsets 主数据库 字符集与排序顺序sysconfigures 主数据库 配置 ...
- 【转】ByteBuffer 到底怎么用?网络编程中一点总结!--不错
原文网址:http://cuisuqiang.iteye.com/blog/1443212 做tcp网络编程,要解析一批批的数据,可是数据是通过Socket连接的InputStream一次次读取的,读 ...