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