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 ?

9
  • 1
    Can you give more details? Also I think you can give toJS a try. Commented Oct 15, 2015 at 12:45
  • 1
    jsfiddle, work for me. You even don't need the valueSeq. Could you create a sample to reproduce it? Commented Oct 15, 2015 at 12:45
  • 2
    try this: var array = [] mymap.valueSeq().forEach(function(item){ array.push(item)}) Commented Oct 15, 2015 at 12:45
  • @fuyushimoya you right - back to the drawing board for me to check - thanks Commented Oct 15, 2015 at 12:48
  • 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. Commented Oct 15, 2015 at 13:41

3 Answers 3

26

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

Sign up to request clarification or add additional context in comments.

Comments

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>

1 Comment

map.valueSeq().toArray() vs [...map.values()] which one is faster?
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()]

2 Comments

Are you talking about immutable.js Map, or ES6 Map?
Immutable.js. See the links in my answer. This does work with Immutable.js. @mjuopperi if you're finding otherwise, please clarify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.