28

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 ?

LenW
  • 3,054
  • 1
  • 24
  • 25
  • 1
    Can you give more details? Also I think you can give `toJS` a try. – Herrington Darkholme Oct 15 '15 at 12:45
  • 1
    [jsfiddle](https://jsfiddle.net/3sjq148f/1), work for me. You even don't need the `valueSeq`. Could you create a sample to reproduce it? – fuyushimoya Oct 15 '15 at 12:45
  • 2
    try this: `var array = [] mymap.valueSeq().forEach(function(item){ array.push(item)})` – javaguest Oct 15 '15 at 12:45
  • @fuyushimoya you right - back to the drawing board for me to check - thanks – LenW Oct 15 '15 at 12:48
  • @fuyushimoya I added an example above. The array of objects becomes a List of Maps afaics – LenW Oct 15 '15 at 13:30
  • @LenW, if you see its doc, Map's constructor seems don't accept `array` of `object`. It either accept Object like `{key: value}` or `array` of `array`: `[[key, value]]`, but not `[{key: value}]`. – fuyushimoya Oct 15 '15 at 13:37
  • @fuyushimoya thats why I am using `fromJS` ? Is that wrong ? – LenW Oct 15 '15 at 13:39
  • 1
    Oops, missed that, but then I believe it becomes a `Collection` of `Map`. Which means `is.valueSeq().toArray()` gives you an array of `Map`. You need to unwrap it further, like [this](http://jsfiddle.net/3sjq148f/3/). – fuyushimoya Oct 15 '15 at 13:41
  • @fuyushimoya is you put that as an answer then I can mark it correct. thanks all, it is the `toJS` that I was missing for the nested Maps – LenW Oct 15 '15 at 13:45

3 Answers3

26

Just use someMap.toIndexedSeq().toArray() for getting an array of only values.

Hüseyin Zengin
  • 1,216
  • 11
  • 23
23

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>
fuyushimoya
  • 9,715
  • 3
  • 27
  • 34
10

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()]

ericsoco
  • 24,913
  • 29
  • 97
  • 127