Map 转为数组
const map = new Map([
['name','张三'],
[1,'one'],
[2,'two'],
[3,'three'],
]);
console.log(map);//{'name' => '张三', 1 => 'one', 2 => 'two', 3 => 'three'}
let map0 = [...map.keys()];
console.log(map0); //['name', 1, 2, 3]
let map1 = [...map.values()];
console.log(map1); //['张三', 'one', 'two', 'three']
// es版本过低不支持 [...map.values()]
let map1 = Array.from(map.values());
console.log(map1); //['张三', 'one', 'two', 'three']
let map2 = [...map.entries()];
console.log(map2); //[Array(2), Array(2), Array(2), Array(2)]
//0: (2) ['name', '张三']
//1: (2) [1, 'one']
//2: (2) [2, 'two']
//3: (2) [3, 'three']
// length: 4
const arrayFromMap = Array.from(map); 或 [...map]
输出
[
['name','张三'],
[1,'one'],
[2,'two'],
[3,'three'],
]
数组 转为 Map
let str = [[1,'one'],[2,'two'],[3,'three'],];
let str0 = new Map(str);
console.log(str0); // {1 => 'one', 2 => 'two', 3 => 'three'}
let dome = new Map();
dome.set(‘name’,[1,2,3]) //存
dome.get(‘name’) //取 [1,2,3]