码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] Modifying an Immutable.js Map()

时间:2015-10-18 19:45:22      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

We will now look at five methods that modify an Immutable.Map().

  • set
  • update
  • delete
  • clear
  • merge
//set()
var map = Immutable.Map();
var todo = {
  id: +new Date(),
  name: "todo1",
  content: "learning Immutable"
}
map = map.set(todo.id, todo);
var task = map.get(todo.id);
console.log(task.content); //"learning Immutable"

//update
var map = Immutable.Map();
var todo = {
  id: +new Date(),
  name: "todo1",
  content: "learning Immutable"
}
map = map.set(todo.id, todo);
todo.content = "RxJS";
var task = map.update(todo.id, function(todo){
  return todo;
});
console.log(task.get(todo.id).content); //"RxJS"

//delete
var map = Immutable.Map();
var todo = {
  id: +new Date(),
  name: "todo1",
  content: "learning Immutable"
};
map = map.delete(todo.id, todo);
console.log(map.size); // 0

//clear
var map = Immutable.Map();

var todo1= {
  id: +new Date(),
  name: "todo1",
  content: "learning Immutable"
};

var todo2= {
  id: +new Date() + 1000,
  name: "todo1",
  content: "learning Immutable"
};

map = map.set(todo1.id, todo1);
map = map.set(todo2.id, todo2);
console.log(map.size); //2
map = map.clear();
console.log(map.size); //0

//merge
var map1 = Immutable.Map({a: ‘10‘});
var map2 = Immutable.Map({b: ‘20‘});

map = map1.merge(map2); 
console.log(map.toString()); //"Map { \"a\": \"10\", \"b\": \"20\" }"

var map1 = Immutable.Map({a: ‘10‘});
var map2 = Immutable.Map({a: ‘20‘});

map = map1.merge(map2); 
console.log(map.toString()); //"Map { \"a\": \"20\" }"

 

[Javascript] Modifying an Immutable.js Map()

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4889963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!