Object.entries()
and Object.values()
This chapter describes the ECMAScript 2017 feature “Object.values/Object.entries” by Jordan Harband.
Object.entries()
Object.values()
Object.entries()
This method has the following signature:
If a JavaScript data structure has keys and values then an entry is a key-value pair, encoded as a 2-element Array. Object.entries(x)
coerces x
to an Object and returns the entries of its enumerable own string-keyed properties, in an Array:
Properties, whose keys are symbols, are ignored:
Object.entries()
finally gives us a way to iterate over the properties of an object (read here why objects aren’t iterable by default):
Object.entries()
Object.entries()
also lets you set up a Map via an object. This is more concise than using an Array of 2-element Arrays, but keys can only be strings.
Object.entries()
Object.entries()
an Array and not an iterator?Object.keys()
, not, e.g., Map.prototype.entries()
.Object.entries()
only return the enumerable own string-keyed properties?Object.keys()
. That method also ignores properties whose keys are symbols. Eventually, there may be a method Reflect.ownEntries()
that returns all own properties.Object.values()
Object.values()
has the following signature:
It works much like Object.entries()
, but, as its name suggests, it only returns the values of the own enumerable string-keyed properties: