The JavaScript Object.entries() method
The Object.entries() method returns an array of an object's [key, value] pairs. Object.entries({a: 1}) gives [["a", 1]]. It's the cleanest way to loop over an object when you need both the key and its value, especially with a for...of loop and destructuring.
Overview
Object.entries() gives you everything at once: an array where each item is a [key, value] pair. That structure is built for looping over an object when you need the name and the value together — which is most of the time.
The idiomatic pattern combines it with a for...of loop and destructuring: for (const [key, value] of Object.entries(obj)) reads almost like English. You can also map() over the entries to transform an object, or filter them before rebuilding.
Its perfect partner is Object.fromEntries(), which does the reverse — turns an array of pairs back into an object. Together they let you treat an object like an array: entries() to break it open, array methods to transform, fromEntries() to put it back together. For keys or values alone, use Object.keys() or Object.values().
Syntax
const pairs = Object.entries(obj)
for (const [key, value] of Object.entries(obj)) {
// use key and value
}
Parameters
The Object.entries() method accepts the following parameters.
| Parameter | Description |
|---|---|
obj |
The object whose own enumerable [key, value] pairs you want as an array. |
Example
<ul id="list" style="font:15px system-ui,sans-serif"></ul>
<script>
const settings = { theme: 'dark', font: 'sans', size: 16 };
const list = document.getElementById('list');
for (const [key, value] of Object.entries(settings)) {
const li = document.createElement('li');
li.textContent = key + ' = ' + value;
list.appendChild(li);
}
</script>
Best practices
- Loop with
for (const [key, value] of Object.entries(obj))to read both at once. - Pair it with
Object.fromEntries()to transform an object via array methods and rebuild it. - Use destructuring in the loop to name the key and value clearly.
- For keys or values alone, the simpler Object.keys() /
Object.values()are enough.
Frequently asked questions
What does Object.entries() return?
[key, value] pairs for the object's own enumerable properties.How do I loop over an object's keys and values?
for (const [key, value] of Object.entries(obj)) { ... }.How do I convert an array of pairs back into an object?
Object.fromEntries(pairs) — the inverse of Object.entries().