Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Object.entries

Returns an array of property name and value pairs.

Available since: ES2017

Syntax

Object.entries(obj)

Parameters

ParameterTypeDescription
objobjectThe 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: 25

Object.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

Thanks for reading!

Donate to the site — we work hard for you.