Object.fromEntries()
in depth
- it is a static function defined on
Object
the key-value pairs might be given as
- a simple array, with (at least) two values
- a simple array, with multiple key-value pairs
- a simple array, with duplicate keys override their predecessor
- an object, with properties
0
and1
- an object, without a property
0
results in an object usingundefined
as key - many objects, with the same property
0
override each other`s values - a
Map
, which maps naturally to object key+value - an empty
Map
, results in an empty object - a
Set
, used withset.entires()
, returns an object with key=value
the 1st parameter
must be iterable
- like an array
- a self-made iterable
each entry must have a property 0
and 1
for key+value
- like an object with these explicit properties
- if any (or both) are missing,
undefined
is used - if an empty array is given, both are undefined too
- toString-ables can be keys
- iterables (like a
Map
) are NOT expected, and not used
throws a TypeError
- if missing
- if
undefined
(which is not an iterable) - if
null
(which is not an iterable) - if boolean (which is not an iterable)
- if a Symbol (which is not an iterable)
more use cases/learnings, are
- can be used to map data
- an empty string, returns an empty object
- a single-space string, throws
not symetric to Object.entries()
- allows Symbols as keys, while
Object.entries()
does not report them
Links
Description of Object.fromEntries() on MDN.
The specification describing `Object.fromEntries()`.
The (now archived) proposal, before it went into the spec, interesting read if you wanna go deep.
Very interesting details about some decisions on this method.