不可变JS更新地图中的所有对象

时间:2017-03-23 15:06:38

标签: javascript reactjs redux immutable.js

我有这张地图:

map1:{
    x: {...}
    y: {...}
    h: {...}
    j: {...}
    k: {...}
}

如何更新公共财产,例如"选择"对于地图中的所有对象,尽可能最快地使用不可变的js反应redux js?

感谢。

1 个答案:

答案 0 :(得分:0)

const { fromJS } = require('immutable');

let state = fromJS({
  map1: {
    x: { selected: false},
    y: { selected: false}
  }
});

console.log(state.update('map1', item => item.map( 
  keyValue => keyValue.set('selected', true)
)).toJS());

//result:{ map1: { x: { selected: true }, y: { selected: true } } }

//Have not tried other different methods to test speed, 
//since the field is not deep-nested, it should be fast enough, Hope this may help, :)