HOWTO · JavaScript
How to Loop Through Dictionary in JavaScript
Learn how to loop through dictionaries in JavaScript with various methods like for...in, Object.keys(), and Object.entries(). This guide provides clear code examples and explanations to help you efficiently extract key-value pairs. Enhance your JavaScript skills today!
On this page
A JavaScript “dictionary” is usually an object used to store key-value data. You can loop through its keys, read values through those keys, or iterate over key-value pairs directly. The right pattern depends on whether inherited properties should be included and whether you need keys, values, or both.
Loop Through an Object with for...in
A for...in loop visits an object’s enumerable string-keyed properties. That includes enumerable properties inherited through its prototype chain, so use an own-property check when you only want data stored directly on the object.
Run this example:
const dictionary = { name: "Ada", role: "engineer", city: "London" };
for (const key in dictionary) {
console.log(`${key}: ${dictionary[key]}`);
}
name: Ada
role: engineer
city: London
The supplied runtime case for this example completed successfully; its output is shown above. If the object may have inherited enumerable properties, guard the loop like this: if (Object.prototype.hasOwnProperty.call(dictionary, key)). The check keeps the body focused on the object’s own properties while retaining the loop form.
Loop Through an Object’s Own Keys with Object.keys()
Object.keys() returns an array containing an object’s own enumerable string-keyed property names. Iterate over that array when you need each key and want to retrieve its value from the object. Inherited properties are not included.
const dictionary = { name: "Ada", role: "engineer", city: "London" };
for (const key of Object.keys(dictionary)) {
console.log(`${key}: ${dictionary[key]}`);
}
name: Ada
role: engineer
city: London
The supplied runtime case for this example completed successfully. Each key is a string, and bracket notation such as dictionary[key] reads the corresponding value. Object.keys() does not include symbol-keyed properties; use an API designed for symbols if those are part of your data model.
Loop Through Key-Value Pairs with Object.entries()
Use Object.entries() when a loop needs both parts of each pair. It returns an array of [key, value] pairs for the object’s own enumerable string-keyed properties. Destructuring names the two values directly in the loop.
const dictionary = { name: "Ada", role: "engineer", city: "London" };
Object.entries(dictionary).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});
name: Ada
role: engineer
city: London
The supplied runtime case for this example completed successfully. Like Object.keys(), Object.entries() skips inherited, non-enumerable, and symbol-keyed properties. For example, a property added with Object.defineProperty(dictionary, "internalId", { value: 7 }) is non-enumerable by default and will not appear in the returned pairs unless its enumerable option is set to true.
Use for...of with Object.entries()
A for...of loop can consume the array returned by Object.entries(). This form is useful when the loop body has multiple statements or needs control flow such as continue or break.
const dictionary = { name: "Ada", role: "engineer", city: "London" };
for (const [key, value] of Object.entries(dictionary)) {
console.log(`${key}: ${value}`);
}
name: Ada
role: engineer
city: London
The supplied runtime case for this example completed successfully. It visits the same own enumerable string-keyed pairs as the preceding Object.entries() example; destructuring assigns each pair’s first item to key and second item to value.
Choose a Looping Method
Choose for...in when you specifically want to visit enumerable string properties and are prepared to include or filter inherited properties. Add an own-property check when inherited values are not part of the task. Choose Object.keys() when you need the object’s own keys and will look up values separately. Choose Object.entries() with forEach() or for...of when each iteration needs both a key and its value.
These methods iterate one object’s properties; they do not recursively traverse nested values. If a value is itself an object, such as profile: { city: "London" }, the outer loop gives you that nested object as the value for profile. Access or iterate over its properties separately when the task requires them. For the common case of processing both keys and values from a plain object, for...of with Object.entries() keeps the relationship explicit.