Object.fromEntries
The Object
object has been buffed with useful methods over the past few years. Object.keys
, Object.values
, Object.freeze
, and Object.assign
all address frequently desired functionality. One of the new Object
methods is fromEntries
, which accepts a Map or map-like array nesting and converts it to a useful object literal!
Convert Map to Object
Converting a Map to a key:value object is simple with Object.fromEntries
:
Object.fromEntries(new Map([["a", "b"], ["c", "d"]])); // Object { a: "b", c: "d" }
Convert a Nested Array to Object
Since the simple nested array is much like a Map, you can also do the following:
Object.fromEntries([["a", "b"], ["c", "d"]]); // Object { a: "b", c: "d" }
There are great uses for Maps but there's nothing as amazing as a simple key:value object to store and reference information!