import { List, Map } from 'immutable';
let arr = [1, [2], {a: 3}];
fromJS(arr);
let obj = {a:1, b:2};
fromJS(obj);
List
const list = List([1, 2, 3]);
List.of(1, 2, [3], 4);
增
const originalList = List([ 0 ]);
originalList.set(1, 1);
originalList.set(0, 'overwritten');
originalList.set(2, 2);
List([ 0, 1, 2, 3, 4 ]).insert(10000, 5)
List([ 0, 1, 2, 3, 4 ]).insert(2, 5)
List([ 1, 2, 3, 4 ]).push(5)
List([ 1, 2, 3, 4 ]).push(5, 6, 7)
List([ 2, 3, 4]).unshift(1);
删
List([ 0, 1, 2, 3, 4 ]).delete(0);
List([ 1, 2, 3, 4 ]).clear()
List([ 1, 2, 3, 4 ]).pop()
List([ 0, 1, 2, 3, 4 ]).shift();
改
const list = List([ 'a', 'b', 'c' ])
const result = list.update(2, val => val.toUpperCase())
查
get<NSV>(index: number, notSetValue: NSV): T | NSV
get(index: number): T | undefined
Map
const { Map } = require('immutable')
Map({ key: "value" })
let obj = { 1: "one", b: 2 }
Object.keys(obj)
obj["1"]
obj[1]
obj.1
obj.b
let map = Map(obj)
map.get("1")
map.get(1)
let obj = {a: [1, 2, 3]}
let imObj1 = Immutable.Map(obj)
let imObj2 = Immutable.fromJS(obj)
imObj1.get('a')
imObj2.get('a')
增
const { Map } = require('immutable')
const originalMap = Map()
const newerMap = originalMap.set('key', 'value')
const newestMap = newerMap.set('key', 'newer value')
originalMap
newerMap
newestMap
const one = Map({ a: 10, b: 20, c: 30 })
const two = Map({ b: 40, a: 50, d: 60 })
one.merge(two)
two.merge(one)
const one = Map({ a: 10, b: 20, c: 30 })
const two = Map({ b: 40, a: 50, d: 60 })
one.mergeWith((oldVal, newVal) => oldVal / newVal, two)
two.mergeWith((oldVal, newVal) => oldVal / newVal, one)
const one = Map({ a: Map({ x: 10, y: 10 }), b: Map({ x: 20, y: 50 }) })
const two = Map({ a: Map({ x: 2 }), b: Map({ y: 5 }), c: Map({ z: 3 }) })
one.mergeDeep(two)
删
const originalMap = Map({
key: 'value',
otherKey: 'other value'
})
originalMap.delete('otherKey')
Map({ key: 'value' }).clear()
改
const aMap = Map({ key: 'value' })
const newMap = aMap.update('key', value => value + value)
const aMap = Map({ nestedList: List([ 1, 2, 3 ]) })
const newMap = aMap.update('nestedList', list => list.push(4))
const aMap = Map({ key: 'value' })
const newMap = aMap.update('noKey', 'no value', value => value + value)
查
get<NSV>(key: K, notSetValue: NSV): V | NSV
get(key: K): V | undefined
Deep persistent changes
setIn()
deleteIn()
updateIn()
mergeIn()
mergeDeepIn()
高阶函数
concat()
map()
flatMap()
filter()
reduce()
转为js
toJS()
toJSON()
toArray()
toObject()
遍历
forEach(
sideEffect: (value: V, key: K, iter: this) => any,
context?: any
): number