Immutable.js Map values to array
Just use someMap.toIndexedSeq().toArray()
for getting an array of only values.
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 Mapvar is = Immutable.fromJS(sr);console.log(sr);console.log(is.toArray());// Now its Array of Mapvar 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>
Map.values()
returns an ES6 Iterable (as do Map.keys()
and Map.entries()
), and therefore you can convert to an array with Array.from()
or the spread operator (as described in this answer).
e.g.:
Array.from(map.values())
or just
[...map.values()]