美文网首页
Immutablejs Map setIn: Plain Jav

Immutablejs Map setIn: Plain Jav

作者: kinkyboots | 来源:发表于2017-12-18 08:34 被阅读33次

Immutable.js offical document of Map.setIn():

https://facebook.github.io/immutable-js/docs/#/Map/setIn

Plain JavaScript Object or Arrays may be nested within an Immutable.js Collection, and setIn() can update those values as well, treating them immutably by creating new copies of those values with the changes applied.

const { Map } = require('immutable@4.0.0-rc.9')
const originalMap = Map({
  subObject: {
    subKey: 'subvalue',
    subSubObject: {
      subSubKey: 'subSubValue'
    }
  }
})

originalMap.setIn(['subObject', 'subKey'], 'ha ha!')
// Map {
//   "subObject": {
//     subKey: "ha ha!",
//     subSubObject: { subSubKey: "subSubValue" }
//   }
// }

Local code causes error:

const Immutable = require('immutable')
var map1 = Immutable.Map({ 'selector': { 'type': 'bar' }});
var map2 = map1.setIn(['selector', 'type'], 'foo');
console.log(map2.toJS());
node_modules/immutable/dist/immutable.js:870
    if (!condition) throw new Error(error);
                    ^
Error: invalid keyPath

Cause:

Map() function does not deeply convert JavaScript Objects and Arrays to Maps and Lists, use fromJS() instead.
https://stackoverflow.com/questions/37712871/why-does-immutable-js-throw-invalid-key-path-on-map-setin
https://stackoverflow.com/questions/39170433/immutable-js-getin-returns-undefined-but-works-in-console-from-the-browser

相关文章

网友评论

      本文标题:Immutablejs Map setIn: Plain Jav

      本文链接:https://www.haomeiwen.com/subject/bwjpwxtx.html