在调试代码时发现:JavaScript ES6 Map 对象,JSON.stringify(mapObject)打印不出来,需要用…mapObject.entries()才可以
下面是打印的一些测试结果:
const mapObject = new Map();
mapObject.set(1, 'hello');
mapObject.set(2,'hello2');
console.log(JSON.stringify(mapObject));
// {}
console.log(JSON.stringify(...mapObject.entries()));
//[1,"hello"],这里如果不加中括号,只能打印出第一个
console.log(JSON.stringify([...mapObject.entries()]));
//[[1,"hello"],[2,"hello2"]]
console.log([...mapObject.entries()]);
// [[1, "hello"],[2, 'hello2']]
console.log([...mapObject.keys()]);
// [1,2]
console.log([...mapObject.values()]);
// ["hello","hello2"]