JS: Convert Object to Map 🚀

By Xah Lee. Date: . Last updated: .

Convert Object to Map (require JS2017)

/*
xah_obj_to_map(obj) convert obj to map datatype.
Return a map instance. The input obj is not changed.
Only keys converted are: own property, enumerable, string keys.

Require JS2017

Version: 2018-02-02
*/

const xah_obj_to_map = (obj) => (new Map(Object.entries(obj)));

// HHHH------------------------------
// test

const xx = { "a": 2, "b": 9, [Symbol()]: "symbol" };

console.log(xah_obj_to_map(xx));
// Map { "a" => 2, "b" => 9 }

Pre-JS2015 Version

JavaScript, Map Object