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\" }"
时间: 2024-10-09 22:40:52