getIn()在Immutable.js中做了什么? [英] what does getIn() do in Immutable.js?

查看:758
本文介绍了getIn()在Immutable.js中做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是文档非常稀疏:

Unfortunately the documentation is very sparse :

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

有没有人有一个例子吗?我是猜测如果我有这样的myObject:

Does anyone have an example? I am guessing that if I have a myObject like so :

  a: {
    b:{
      c:"banana"
    }
  }


myObject.getIn([a,b,c])

myObject.getIn(["a", "b", "c"])

将返回banana。

但是,不可变对象也可以是映射对象,这让我彻底迷茫。

However the immutable objects can also be map objects which leaves me thoroughly confused.

推荐答案

短暂

map.getIn([a,b,c]) map.get(a)的快捷方式.get(b ).get(c)

详细信息:

你可能已经进入了一个 fromJS 陷阱。致电:

You have probably got into one of the fromJS traps. Calling :

const map = Immutable.fromJS({a: {b: {c: "banana"}}});

仅使用键<$创建地图 c $ c> a ,其值也是地图,只有键 b ,它的值也是地图,只有键 c 和值 banana

creates a Map with only key a, which's value is also a Map with only key b, which's value is also a Map with only key c and value banana.

用其他词语 fromJS 进入所提供对象的深处,并定义每个对象的地图和每个列表数组

With other words fromJS goes into the deep of the object provided, and defines Map for each Object and a List for each Array

使用此示例,调用 map.getIn([a,b,c ]) map.get(a)的快捷方式.get(b)。get(c)

但如果你将 map 定义为 Map

const map = new Immutable.Map({a: {b: {c: "banana"}}});

它会创建一个地图,只有 a ,其值是普通对象 {b:{c:banana}} ,并调用 map.get(a)。 get(b)。get(c)会抛出像这样的东西,而不是函数,因为 map.get(a)将返回 {b:...} 普通对象。

it creates a Map, with only key a, which's value is plain object {b: {c: "banana"}}, and calling a map.get("a").get("b").get("c") will throw you something like get is not a function, since map.get("a") will return {b: ...} plain object.

同样的原因是为什么 map.getIn([a,b,c])将无法正常工作。

Same reasons are why map.getIn(["a", "b", "c"]) will not work as you might expect.

这篇关于getIn()在Immutable.js中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆