Converts a Map
to an object.
- Use
Map.prototype.entries()
to convert theMap
to an array of key-value pairs. - Use
Object.fromEntries()
to convert the array to an object.
代码实现
const mapToObject = map => Object.fromEntries(map.entries());
mapToObject(new Map([['a', 1], ['b', 2]])); // {a: 1, b: 2}
翻译自:https://www.30secondsofcode.org/js/s/convert-map-to-object