immutable.js: update

This commit is contained in:
Rico Sta. Cruz 2017-10-27 12:50:39 +08:00
parent d557761007
commit d5a0603436
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 9 additions and 9 deletions

View File

@ -1,24 +1,22 @@
---
title: Immutable.js
category: JavaScript libraries
layout: default-ad
layout: 2017/sheet
---
```js
var Immutable = require('immutable')
```
## Maps
### Maps
```js
var map = Immutable.Map({ a: 1, b: 2, c: 3 })
```
```js
map
.set('b', 50)
.get('b') // 50
```
## Lists
### Lists
```js
var list = Immutable.List.of(1, 2)
@ -31,15 +29,17 @@ list
.size
```
## Nested maps
### Nested maps
```js
var nested = Immutable.fromJS({ user: { profile: { name: 'John' } } })
nested
// Update
.mergeDeep({ user: { profile: { age: 90 } } })
.setIn([ 'user', 'profile', 'name' ], 'Jack')
.updateIn([ 'user', 'profile', 'name' ], (s) => s.toUpperCase())
// Get
.getIn(['user', 'profile', 'name']) // 'JACK'
```