JS: Pre-JS2015 Patch Functions

By Xah Lee. Date: .

Convert Object to Map (no require JS2015)

Here is a function that converts object to map data type.

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

Version: 2018-02-02
*/
const xah_obj_to_map = ((zobj) => {
  const mp = new Map();
  Object.keys(zobj).forEach((k) => {
    mp.set(k, zobj[k]);
  });
  return mp;
});

// s------------------------------
// test

const xx = { "a": 2, "b": 9, [Symbol()]: "symbol" };
console.log(xah_obj_to_map(xx));
// Map(2) { "a" => 2, "b" => 9 }