[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 ...
随机推荐
- CodeIgniter框架介绍
入口 index.php :一些路径常量定义: => CodeIgniter.php : 1. 初始化一些 system/core内的类,例如Config, Utf8, URI 和Router等 ...
- favicon.ico的使用方法
favicon.ico怎么来,就自己决定了. 虽说是放在网站根目录下就行了, 但最好是放在网站images目录下,方便统一管理. 然后在head.tpl.php 中的<head></ ...
- oracle系列索引
今天终于把oracle入门的知识通篇过了一遍. 一篇文章没有写,先做个索引.把知识系统的梳理下. 数据库基本概念-oracle介绍 oracle安装,配置,启动 oracle工具 sqlplus 用户 ...
- 2016030204 - git和github结合
1.下载和安装git客户端 参考:http://www.cnblogs.com/zhtzyh2012/p/5232291.html 2.github上创建项目 参考:http://www.cnblog ...
- windows“画图”工具用法
图片编辑工具不少,photoshop功能强大,但是并不是那么容易掌握.而且,倘若一个简单的图片处理问题就使用这么强大的工具,有点“杀鸡焉用牛刀”的意思,要知道只打开photoshop就需要一段等待时间 ...
- PyCharm 5.0.3 快捷键
下面记录PyCharm 5.0.3常用快捷键 注释 注释行:Ctrl+/
- 报错java.net.SocketException: Software caused connection abort: recv failed 怎么办
产生这个异常的原因有多种方面,单就如 Software caused 所示, 是由于程序编写的问题,而不是网络的问题引起的. 已知会导致这种异常的一个场景如下: 客户端和服务端建立tcp的短连接,每次 ...
- KeilC51使用详解 (三)
C51强大功能及其高效率的重要体现之一在于其丰富的可直接调用的库函数,多使用库函数使程序代码简单,结构清晰,易于调试和维护,下面介绍C51的库函数系统. 第一节 本征库函数(intrinsic rou ...
- Git标签管理
一般我们发布一个新版本到线上服务器时都会在版本库中打一个标签,这时就确定了某个版本将发布到线上.我们可以随时可以查看这个打标签的版本,也就 是说标签其实呢,就是版本库中一个快照.简单说标签就是指向某个 ...
- cheerio返回数据格式
通读cheerio API { options: { decodeEntities: false, withDomLvl1: true, normalizeWhitespace: false, xml ...