[Javascript] Modifying an Immutable.js Map()

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

[Javascript] Modifying an Immutable.js Map()的相关文章

[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 array tuples. console.clear(); // Can be an object var map = Immutable.Map({key: "value"}); console.log(map.get("key")); //"value&quo

[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-value pairs. The Immutable.js List() is analogous to a Javascript Array and contains many of the same native methods. Let's compare the two and dive into

[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 it has the all powerful slice()--and unlike Array it offers functional methods like take() and skip(). You get the best of both the imperative and fu

[Immutable,js] Iterating Over an Immutable.js Map()

Immutable.js provides several methods to iterate over an Immutable.Map(). These also apply to the other immutable structures found within the Immutable.js family, such as Set and List. The primary methods are map and forEach, but we will also cover 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.js class contains a prefixed "to" method like Map.toList(), Map.toSet(), etc. Converting these types sometimes results in a loss of data, as we

[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. Objects are converted into maps. Arrays are converted into lists. The fromJS() method can also take a reviver function for custom conversions.  Object to Im

[Javascript] Manage Application State with Immutable.js

Learn how Immutable.js data structures are different from native iterable Javascript data types and why they provide an excellent foundation on which to build your application's state. Instead of them being mutable, they're always immutable, meaning

大话immutable.js

为啥要用immutable.js呢.毫不夸张的说.有了immutable.js(当然也有其他实现库)..才能将react的性能发挥到极致!要是各位看官用过一段时间的react,而没有用immutable那么本文非常适合你. 1,对于react的来说,如果父组建有多个子组建 想象一下这种场景,一个父组建下面一大堆子组建.然后呢,这个父组建re-render.是不是下面的子组建都得跟着re-render.可是很多子组建里面是冤枉的啊!!很多子组建的props 和 state 然而并没有改变啊!!虽然

[Immutable.js] Exploring Sequences and Range() in Immutable.js

Understanding Immutable.js's Map() and List() structures will likely take you as far as you want to go with immutable programming. They have only small semantic differences between each other and the remaining structures in the Immutable.js family. S