How to convert Map keys to array?

前端 未结 7 816
遥遥无期
遥遥无期 2020-12-12 10:32

Lets say I have the following map:

let myMap = new Map().set(\'a\', 1).set(\'b\', 2);

And I want to obtain [\'a\', \'b\'] based on the abov

7条回答
  •  悲哀的现实
    2020-12-12 11:13

    You can use the spread operator to convert Map.keys() iterator in an Array.

    let myMap = new Map().set('a', 1).set('b', 2).set(983, true)
    let keys = [...myMap.keys()]
    console.log(keys)

提交回复
热议问题