Key-Value Arrays
Object.entries returns an array of [key, value] pairs.
It is great for looping and converting objects.
JavaScript Tutorial
Object.entries returns an array of key-value pairs for an object.
It is useful for iteration, filtering, and transformations.
Many object transformations are easier with entries and Object.fromEntries.
This pattern keeps object logic readable and functional.
Object.entries(obj)
Object.fromEntries(entries)const user = { name: "Ava", role: "admin" };
const entries = Object.entries(user);
console.log(entries);Returns [['name','Ava'], ['role','admin']].
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.
Object.entries returns an array of [key, value] pairs.
It is great for looping and converting objects.
Combine entries with map or filter to transform objects.
Use Object.fromEntries to turn pairs back into an object.
Use for...of to iterate entries cleanly.
Destructure [key, value] in the loop.
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.
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.
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));Do not rely on property order for logic.
Use Object.fromEntries to rebuild objects.
Arrays produce index-value pairs; use array methods instead.
An array of [key, value] pairs.
Use Object.fromEntries.
It simplifies object iteration and transformation.
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);An array of [key, value] pairs.
Use Object.fromEntries.
No, only own enumerable properties.
Try changing values and see how entries update.