JavaScript Reference ยท Object
Object.entries()
Reference for JavaScript Object.entries(): get an array of [key, value] pairs, loop over objects, transform them with map() and convert back with fromEntries().
Object.entries() returns an array with one [key, value] pair for every own, enumerable, string-keyed property of an object. It is the quickest way to loop over an object's keys and values together, and it gives you access to array methods such as map(), filter() and sort().
Together with Object.fromEntries(), it lets you transform an object: convert to entries, change them, and convert back.
Object.entries({ a: 1, b: 2 }); // [["a", 1], ["b", 2]]
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Object.entries(obj)
| Name | Description |
|---|---|
obj |
The object whose own enumerable string-keyed properties are listed. Primitives are converted to objects first. |
Return value: a new array of [key, value] arrays. Each key is a string.
Exceptions: a TypeError if obj is null or undefined.
Usage notes
- Only own, enumerable, string keys. Inherited properties, non-enumerable properties and symbol keys are skipped.
- Order: integer-like keys come first in ascending numeric order, then other string keys in the order they were created. This is the same order as
for...infor own properties andObject.keys(). - Values are not copied deeply. The pairs hold the same values, so objects inside are shared with the original. Changing the array of pairs does not change the object.
- Strings and arrays work too. A string gives its characters with index keys, an array gives
["0", value]pairs. Note that the keys are strings, not numbers. - Destructuring in the callback (
([key, value]) => ...) keeps code readable. Use[, value]to skip the key. - Maps:
new Map(Object.entries(obj))converts an object to aMap, andObject.fromEntries(map)goes back. AMapitself has its ownentries()method. - Large objects: it creates one array per property, so for very hot code paths a
for...inloop withObject.hasOwn()allocates less.
Browser support
Object.entries() is available in every current browser and JavaScript runtime.