JSON转换Map:如果是JSON字符串必须使用JSON.parse()转为对象;使用 Object.entries() 接受对象返回二维数组;let arr = Object.entries({"name":"张三","age":"18","address":"xian"}); console.log(arr) // [["name","张三"],["age","18"],["address","xian"]] //调用Map()构造函数 let arr = Object.entries({"name":"张三","age":"18","address":"xian"}); let map = new Map(arr); console.log(map); // {'name' => '张三', 'age' => '18', 'address' => 'xian'} //JSON转换Map: let jsonRecord:Record<string,Object> = JSON.parse('{ "0": {"num": 1}, "1": {"num": 2} }') as Record<string,Object>; let map:Map<string,Object> = new Map(); Object.keys(jsonRecord).forEach((key) => { console.log(key + ': ' + jsonRecord[key]); map.set(key,jsonRecord[key]); });
JSON转换Map:如果是JSON字符串必须使用JSON.parse()转为对象;
使用 Object.entries() 接受对象返回二维数组;