How to convert Map keys to array?

前端 未结 7 806
遥遥无期
遥遥无期 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:15

    Not exactly best answer to question but this trick new Array(...someMap) saved me couple of times when I need both key and value to generate needed array. For example when there is need to create react components from Map object based on both key and value values.

      let map = new Map();
      map.set("1", 1);
      map.set("2", 2);
      console.log(new Array(...map).map(pairs => pairs[0])); -> ["1", "2"]
    

提交回复
热议问题