Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript Object.entries()

Object.entries returns an array of key-value pairs for an object.

It is useful for iteration, filtering, and transformations.

Why We Need It

Many object transformations are easier with entries and Object.fromEntries.

This pattern keeps object logic readable and functional.

Syntax

Object.entries(obj)
Object.fromEntries(entries)

Basic Example

1. Basic entries

const user = { name: "Ava", role: "admin" };
const entries = Object.entries(user);

console.log(entries);

Returns [['name','Ava'], ['role','admin']].

Real World Example

2. Loop entries

const settings = { theme: "dark", layout: "grid" };
for (const [key, value] of Object.entries(settings)) {
  console.log(key, value);
}

Destructure key and value in a loop.

Multiple Use Cases

Key-Value Arrays

Object.entries returns an array of [key, value] pairs.

It is great for looping and converting objects.

Transform Objects

Combine entries with map or filter to transform objects.

Use Object.fromEntries to turn pairs back into an object.

Iteration

Use for...of to iterate entries cleanly.

Destructure [key, value] in the loop.

More Examples

3. Transform values

const prices = { a: 10, b: 20 };
const doubled = Object.fromEntries(
  Object.entries(prices).map(([k, v]) => [k, v * 2])
);

console.log(doubled);

Map over entries to transform an object.

4. Filter entries

const data = { a: 1, b: 0, c: 3 };
const filtered = Object.fromEntries(
  Object.entries(data).filter(([, v]) => v > 0)
);

console.log(filtered);

Filter object properties by value.

Comparison

Without

const result = {};
for (const key in obj) {
  if (Object.hasOwn(obj, key)) {
    result[key] = obj[key];
  }
}

With

const result = Object.fromEntries(Object.entries(obj));

Common Mistakes and Fixes

Expecting ordered keys

Do not rely on property order for logic.

Forgetting fromEntries

Use Object.fromEntries to rebuild objects.

Using entries for arrays

Arrays produce index-value pairs; use array methods instead.

Interview Questions

What does Object.entries return?

An array of [key, value] pairs.

How do you rebuild an object?

Use Object.fromEntries.

Why use entries?

It simplifies object iteration and transformation.

Practice Problem

Practice: Convert an object to entries, double the values, and rebuild it.

const data = { a: 2, b: 3 };
// TODO: double values using entries

One Possible Solution

const data = { a: 2, b: 3 };
const doubled = Object.fromEntries(
  Object.entries(data).map(([k, v]) => [k, v * 2])
);
console.log(doubled);

Frequently Asked Questions

What does Object.entries return?

An array of [key, value] pairs.

How do I convert back to object?

Use Object.fromEntries.

Does entries include inherited properties?

No, only own enumerable properties.

Try It Yourself

Try changing values and see how entries update.