Immutable.js将值映射到数组 [英] Immutable.js Map values to array
问题描述
我正在使用 http://facebook.github.io中的不可变地图/ immutable -js / docs /#/ map
我需要获取一组值来传递给后端服务,我想我是遗漏一些基本的东西,我该怎么办?
我试过:
mymap.valueSeq()。toArray()
但我仍然得到一个不可变的数据结构?
例如:
var d ='[{address:10.0.35.118,的CPU:4}]';
var sr = JSON.parse(d);
var is = Immutable.fromJS(sr);
console.log(sr);
console.log(is.toArray());
console.log(is.valueSeq()。toArray());
请参阅 http://jsfiddle.net/3sjq148f/2/
我们从不可变数据结构返回的数组似乎仍然用每个包含对象的不可变字段装饰。这是预期的吗?
这是因为 sr
是一个数组
对象
,所以如果你使用 .fromJS
来转换它,它变成列表
地图
。
is.valueSeq()。toArray();
(此处不需要 valueSeq
。)将其转换为数组
Map
,所以你需要遍历数组,并转换每个 Map
item to Array
。
var d ='[{address :10.0.35.118,cpus:4}]'; var sr = JSON.parse(d); // Object of Object => Mapvar列表是= Immutable.fromJS(sr); console.log(sr); console.log(is.toArray()); //现在它的Mapvar列表= is.valueSeq()。toArray();控制台.log(list); list.forEach(function(item){//将Map转换为Array console.log(item.toArray());});
< pre class =snippet-code-html lang-html prettyprint-override> < script src =https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.5/immutable .min.js>< / script>
I am using the immutable Map from http://facebook.github.io/immutable-js/docs/#/Map
I need to get an array of the values out to pass to a backend service and I think I am missing something basic, how do I do it ?
I have tried :
mymap.valueSeq().toArray()
But I still get an immutable data structure back ?
For example :
var d = '[{"address":"10.0.35.118","cpus":4}]';
var sr = JSON.parse(d);
var is = Immutable.fromJS(sr);
console.log(sr);
console.log(is.toArray());
console.log(is.valueSeq().toArray());
See this http://jsfiddle.net/3sjq148f/2/
The array that we get back from the immutable data structure seems to still be adorned with the immutable fields for each contained object. Is that to be expected ?
It's because the sr
is an Array
of Object
, so if you use .fromJS
to convert it, it becomes List
of Map
.
The is.valueSeq().toArray();
(valueSeq
is not necessary here.) converts it to Array
of Map
, so you need to loop through the array, and convert each Map
item to Array
.
var d = '[{"address":"10.0.35.118","cpus":4}]';
var sr = JSON.parse(d);
// Array of Object => List of Map
var is = Immutable.fromJS(sr);
console.log(sr);
console.log(is.toArray());
// Now its Array of Map
var list = is.valueSeq().toArray();
console.log(list);
list.forEach(function(item) {
// Convert Map to Array
console.log(item.toArray());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.5/immutable.min.js"></script>
这篇关于Immutable.js将值映射到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!