Immutablejs Map setIn: Plain Javascript object Error: invalid keyPath

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这里先提一下for in 循环,因为之前在C++ 和 OC中用到过快速遍历,但是发现JavaScript有所不同,...
    Allen_HD阅读 316评论 0 1
  • 牛二心花放,这时候的他浑身舒坦,迈着轻盈的脚步,哼着小曲《情僧偷度西湘馆》一屁一颠地走回家。为啥?出国旅游签证办妥...
    天亦阅读 637评论 2 4
  • 腾讯自从做起了游戏,一路走来,可谓是没少挨黑,各种黑历史帮它聚集了一批忠实的“黑粉”。抄袭,可能是它最耀眼的标签,...
    晴空Qing阅读 1,847评论 0 2
  • 不要再找了,你正在查找的东西你早就有了。 为了帮助自己获得更多的活力、更多的自我控制、或者仅仅为了感受更多的满足,...
    Jodoo阅读 21,984评论 90 944