您的位置:首页 > Web前端 > JavaScript

[Javascript] Modifying an Immutable.js Map()

2015-10-18 18:16 155 查看
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\" }"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: