Javascript 在 Immutable.js 中获取嵌套值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33910300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting nested values in Immutable.js
提问by xiankai
According to the docs here: https://facebook.github.io/immutable-js/docs/#/Map/getIn
根据这里的文档:https: //facebook.github.io/immutable-js/docs/#/Map/getIn
I should be able to get the deeply nested value by providing an array for the keyPath
argument. This is what I've done, however I'm getting undefined
as the return value.
我应该能够通过为keyPath
参数提供一个数组来获得深度嵌套的值。这就是我所做的,但是我得到的undefined
是返回值。
var obj = Immutable.Map({categories: {1: 'a', 2: 'b'}});
var output = obj.getIn(['categories', 1]);
alert(output);
What am I doing wrong?
我究竟做错了什么?
回答by user1440308
A map is simply a collection of keys with values. What you have is a map with the key categories
, and the value {1: 'a', 2: 'b'}
. The value does not automatically become an Immutable map just because you place it inside another map.
映射只是具有值的键的集合。您拥有的是一个带有键categories
和值的映射{1: 'a', 2: 'b'}
。该值不会因为您将其放置在另一个地图中而自动成为不可变地图。
Now, the getIn()
function will only "see" other Immutable.js maps. What you have in there is a regular ol' javascript object. If you just want to get the 1
value, you can just do:
现在,该getIn()
函数只会“看到”其他 Immutable.js 映射。你在那里有一个普通的 ol' javascript 对象。如果你只是想获得1
价值,你可以这样做:
var obj = Immutable.Map({categories: {1: 'a', 2: 'b'}});
var output = obj.getIn(["categories"])[1];
alert(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.min.js"></script>
If you want the categories
collection to also be a map, you'll have to define that explicitly. Also, as Amit pointed out, you'll need to use strings with the getIn()
function.
如果您希望categories
集合也成为地图,则必须明确定义它。此外,正如 Amit 指出的,您需要在getIn()
函数中使用字符串。
var obj = Immutable.Map({
categories: Immutable.Map({
1: 'a',
2: 'b'
})
});
var output = obj.getIn(['categories', '1']);
alert(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.min.js"></script>
The above code will return 'a'
.
上面的代码将返回'a'
.
It would probably be more convenient for you to do something like below, if you have many nested maps.
如果您有许多嵌套地图,那么执行以下操作可能会更方便。
var obj = Immutable.fromJS({
categories: {
1: 'a',
2: 'b'
}
});
var output = obj.getIn(['categories', '1']);
alert(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.min.js"></script>
The fromJs()
function will convert any nested objects into maps or lists automatically for you.
该fromJs()
函数将自动为您将任何嵌套对象转换为地图或列表。
回答by Amit
There are 2 problems with your code:
您的代码有两个问题:
Immutable.Map(...)
doesn't traverse the parameter to create a complex immutable. For that you needImmutable.fromJS(...)
.- Once you do that, you need to use strings in
getIn(...)
, so['categories', '1']
instead of['categories', 1]
.
Immutable.Map(...)
不遍历参数来创建一个复杂的不可变。为此,您需要Immutable.fromJS(...)
.- 一旦你这样做了,你需要在 中使用字符串
getIn(...)
,['categories', '1']
而不是['categories', 1]
.
See working fiddle.
请参阅工作小提琴。