Collection types in Immutable.js have a static of()
method. This method is an alternative to using the collection constructor. The one downside to using the constructor approach is that you have to pass in a JavaScript literal. You have to build and allocate memory for a structure that you're not actually using.
The of()
method uses the arguments that are passed to it as the collection items.
You can use the of()
method to create lists of values as follows:
const myList = List.of(1, 2, 3); console.log('myList', myList.toJS()); // -> myList [ 1, 2, 3 ]
You can use the of()
method to create key-value maps:
const myMap = Map.of( 'a', 1, 'b', 2, 'c', 3 ); console.log('myMap', myMap.toJS()); // -> myMap { a: 1, b: 2, c: 3 }
The trick here is to alternate between the key and value arguments that are passed to of()
.