Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutablejs: One liner code to convert keys of a map into array?

From the docs: Map#keys

I get the keys of a Map and loop through it to transform them into an array. Is there a one line code to cleanly convert these keys into an array?

like image 476
Melvin Avatar asked May 02 '15 12:05

Melvin


2 Answers

You can use keySeq instead of keys, an IndexedSeq has toArray method:

var map = Immutable.fromJS({
  a: 1,
  b: 2,
  c: {
    d: "asdf"
  }
})

var arr = map.keySeq().toArray()
like image 84
OlliM Avatar answered Oct 30 '22 14:10

OlliM


If you can use ES6:

var map = Immutable.fromJS({
  a: 1,
  b: 2,
  c: {
    d: "asdf"
  }
});

var [...arr] = map.keys();
console.log(arr); // ["a", "b", "c"]

Or

var arr = Array.from(map.keys());
console.log(arr); // ["a", "b", "c"]
like image 8
geeklain Avatar answered Oct 30 '22 13:10

geeklain