[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 ...
随机推荐
- mysql update不能直接使用select的结果
在sql server中,我们可是使用以下update语句对表进行更新:update a set a.xx= (select yy from b) ;但是在mysql中,不能直接使用set selec ...
- ubuntu 14.04 安装preforce
官网: http://www.perforce.com/ http://www.perforce.com/support-services 1. 下载相关文件 http://filehost.perf ...
- bzoj 1257: [CQOI2007]余数之和sum 数学 && 枚举
1257: [CQOI2007]余数之和sum Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 1779 Solved: 823[Submit][Sta ...
- Jsp中获得集合List或Set的长度
首先要引入<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ...
- 【BZOJ2434-[Noi2011]】阿狸的打字机(AC自动机(fail树)+离线+树状数组)
Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...
- ServletContextListener作用(转)
ServletContext 被 Servlet 程序用来与 Web 容器通信.例如写日志,转发请求.每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享.因为Context可 ...
- 对于利用ica进行fmri激活区识别的理解
首先,ica是一种探索性的方法,属于数据驱动的范畴. ica计算量很大,一般都是离线式计算. ica基于的猜想是,世界是加性的.在我们所研究的脑科学中,所采集到的BOLD信号,是由一些源信号所构成,更 ...
- Go语言的学习
1.配置环境变量 2.本地阅读报的说明和文档 不用FQ window+R 出现黑窗口 执行 godoc -http :8080 在本地浏览器 localhost:8080 回车 3多行注释 ...
- WebClient的超时问题及解决
WebClient的超时问题及解决 转自:http://blog.163.com/xiaozhi797@126/blog/static/62440288201112245345838/ Webcl ...
- 在QTP中使用DOM
大家对DOM应该都不陌生,它在网页制作中有广泛的应用.如果我们想在QTP中使用DOM模型需要通过Page对象的Object属性来实现.注意,QTP11才有良好的DOM支持操作,这里用百度首页做例子. ...