Object.entries
Returns an array of property name and value pairs.
Available since: ES2017
Syntax
Object.entries(obj)Parameters
| Parameter | Type | Description |
|---|---|---|
| obj | object | The object whose properties to list. |
Return value
ArrayAn array whose every element is an array of a name and a value.
Examples
Looping over properties:
const user = { name: 'Anna', age: 25 };
for (const [key, value] of Object.entries(user)) {
console.log(key + ': ' + value);
}Output:
name: Anna
age: 25Object.fromEntries() builds an object back from pairs:
const pairs = [['a', 1], ['b', 2]];
console.log(Object.fromEntries(pairs));Output:
{
"a": 1,
"b": 2
}Notes
The Object.entries() → map() or filter() → Object.fromEntries() chain lets you transform an object the way you would an array.
See also
- Object.keys — Returns an array of an object's property names.
- Object.fromEntries — Creates an object from a list of key–value pairs.
- Object.values — Returns an array of an object's property values.
- for...of — Loops over the values of arrays, strings, Maps, Sets and other iterables.
Related recipes
In the tutorial
- Iterating objects and Object functions — How to iterate over an object in JavaScript: Object.keys, Object.values, Object.entries, Object.fromEntries, for...in and Object.hasOwn.