18

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?

Melvin
  • 5,798
  • 8
  • 46
  • 55

2 Answers2

39

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()
OlliM
  • 7,023
  • 1
  • 36
  • 47
8

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"]
geeklain
  • 91
  • 1
  • 4