Questions tagged [immutable.js]

Immutable.js provides Persistent Immutable List, Stack, Map, OrderedMap, Set, OrderedSet and Record. They are highly efficient on modern JavaScript VMs (browser and nodejs) by using structural sharing via hash maps tries and vector tries as popularized by Clojure and Scala, minimizing the need to copy or cache data.

Immutable provides immutable data structures like List, Stack, Map, OrderedMap, Set and Record by using persistent hash maps tries and vector tries as popularized by Clojure and Scala. They achieve efficiency on modern JavaScript VMs by using structural sharing and minimizing the need to copy or cache data.

Immutable also provides a lazy Seq, allowing efficient chaining of collection methods like map and filter without creating intermediate representations. Create some Seq with Range and Repeat.

Getting started:

Install immutable using npm.

npm install immutable

Then require it into any module.

var Immutable = require('immutable');
var map = Immutable.Map({a:1, b:2, c:3});

Useful links:

License:

Immutable is BSD-licensed. We also provide an additional patent grant.

1177 questions
385
votes
4 answers

Index inside map() function

I am missing a option how to get the index number inside the map function using List from Immutable.js: var list2 = list1.map(mapper => { a: mapper.a, b: mapper.index??? }).toList(); Documentation shows that map() returns Iterable. Is…
Zygimantas
  • 6,047
  • 7
  • 36
  • 51
231
votes
12 answers

Why is immutability so important (or needed) in JavaScript?

I am currently working on React JS and React Native frameworks. On the half way road I came across Immutability or the Immutable-JS library, when I was reading about Facebook's Flux and Redux implementation. The question is, why is immutability so…
bozzmob
  • 10,774
  • 15
  • 44
  • 67
132
votes
7 answers

How to update element inside List with ImmutableJS?

Here is what official docs said updateIn(keyPath: Array, updater: (value: any) => any): List updateIn(keyPath: Array, notSetValue: any, updater: (value: any) => any): List updateIn(keyPath: Iterable, updater: (value: any)…
Vitalii Korsakov
  • 38,491
  • 18
  • 68
  • 85
61
votes
4 answers

How to set multiple fields at once in Immutable.js Record?

Looking at this, I think that Immutable. Record is the data structure for represent "javascript immutable objects", but I want to update several fields at once without creating several objects calling set each time. I want to do something like…
gabrielgiussi
  • 7,577
  • 6
  • 36
  • 67
51
votes
5 answers

How to use Immutable.js with redux?

Redux framework is using reducers to change app state in response to an action. The key requirement is that a reducer cannot modify an existing state object; it must produce a new object. Bad Example: import { ACTIVATE_LOCATION } from…
Gajus
  • 55,791
  • 58
  • 236
  • 384
50
votes
1 answer

How can I set a deeply nested value in Immutable.js?

When working with plain JavaScript objects it's easy to change a deeply nested object property: people.Thomas.nickname = "Mr. T"; But with Immutable I have to go through each property's ancestors before I have a new people object: var thomas =…
Matt Zeunert
  • 14,721
  • 6
  • 47
  • 77
44
votes
6 answers

How to check if object is Immutable?

Immutable object can be an instance of: Immutable.List Immutable.Map Immutable.OrderedMap Immutable.Set Immutable.OrderedSet Immutable.Stack
Gajus
  • 55,791
  • 58
  • 236
  • 384
43
votes
5 answers

When to use .toJS() with Immutable.js and Flux?

I'm trying to use ImmutableJS with my React / Flux application. My stores are Immutable.Map objects. I'm wondering at which point should I use .toJS() ? Should it be when the store's .get(id) returns ? or in the components with .get('member') ?
chollier
  • 892
  • 1
  • 7
  • 8
41
votes
1 answer

How to describe Immutable.js Map shape with Flow

I would like to describe the shape of a map using Immutable's flow type definitions. You can describe the shape of an object by: const stateShape: { id: number, isActive: boolean } = { id: 123, isActive: true }; Is there something similar…
William
  • 1,379
  • 1
  • 11
  • 25
41
votes
2 answers

What is the difference between ImmutableJS Map() and fromJS()?

var a = {address: {postcode: 5085}} var b = Immutable.fromJS(a) var c = b.setIn(['address', 'suburb'], 'broadview').toJS(); // no error console.log(c); var d = Immutable.Map(a); var e = d.setIn(['address', 'suburb'], 'broadview').toJS(); // error…
sowdri
  • 1,933
  • 4
  • 19
  • 31
36
votes
3 answers

How to get union of several immutable.js Lists

So, I have List a: let a = Immutable.List([1]) and List b: let b = Immutable.List([2, 3]) I want to get List union === List([1, 2, 3]) from them. I try to merge them fist: let union = a.merge(b); // List([2, 3]) It seems like merge method…
Glen Swift
  • 10,962
  • 14
  • 43
  • 73
35
votes
2 answers

How to update a value of a nested object in a reducer?

I have built my state like so const list = { categories: { Professional: { active: false, names: [ { id: 1, name: "Golf", active: false }, { id: 2, name: "Ultimate Frisbee", …
albert
  • 391
  • 1
  • 3
  • 6
35
votes
2 answers

React-Redux complex (deep) state objects

Given my initial redux state is : const state = { currentView: 'ROOMS_VIEW', navbarLinks: List([ {name: 'Rooms', key: 'ROOMS_VIEW'}, {name: 'Dev', key: ''} ]), roomListsSelected: {group: 0, item: 0}, roomLists: [ { name:…
Steven Bayer
  • 1,377
  • 2
  • 11
  • 16
35
votes
6 answers

Why should I use immutablejs over object.freeze?

I have researched on net about the benefits of immutablejs over Object.freeze() but didn't find anything satisfying! My question is why I should use this library and work with non native data structures when I can freeze a plain old javascript…
alisabzevari
  • 7,178
  • 6
  • 38
  • 60
34
votes
3 answers

Performance: Immutable.js Map vs List vs plain JS

Question Is there something wrong with my benchmark? How can Immutable.js find() be 8 times slower than array.find()? Ok, not entirely fair, since I'm using Immutable.Map inside of the Immutable.List. But to me this is a real world example. If I…
Michael
  • 1,494
  • 2
  • 15
  • 31
1
2 3
78 79