tl;dr

  1. In ECMAScript 2017, just call Object.entries(yourObj).
  2. In ECMAScript 2015, it is possible with Maps.
  3. In ECMAScript 5, it is not possible.

ECMAScript 2017

ECMAScript 2017 introduced a new Object.entries function. You can use this to iterate the object as you wanted.

'use strict';

const object = {'a': 1, 'b': 2, 'c' : 3};

for (const [key, value] of Object.entries(object)) {
  console.log(key, value);
}

Output

a 1
b 2
c 3

ECMAScript 2015

In ECMAScript 2015, there is not Object.entries but you can use Map objects instead and iterate over them with Map.prototype.entries. Quoting the example from that page,

var myMap = new Map();
myMap.set("0", "foo");
myMap.set(1, "bar");
myMap.set({}, "baz");

var mapIter = myMap.entries();

console.log(mapIter.next().value); // ["0", "foo"]
console.log(mapIter.next().value); // [1, "bar"]
console.log(mapIter.next().value); // [Object, "baz"]

Or iterate with for..of, like this

'use strict';

var myMap = new Map();
myMap.set("0", "foo");
myMap.set(1, "bar");
myMap.set({}, "baz");

for (const entry of myMap.entries()) {
  console.log(entry);
}

Output

[ '0', 'foo' ]
[ 1, 'bar' ]
[ {}, 'baz' ]

Or

for (const [key, value] of myMap.entries()) {
  console.log(key, value);
}

Output

0 foo
1 bar
{} baz

ECMAScript 5:

No, it's not possible with objects.

You should either iterate with for..in, or Object.keys, like this

for (var key in dictionary) {
    // check if the property/key is defined in the object itself, not in parent
    if (dictionary.hasOwnProperty(key)) {           
        console.log(key, dictionary[key]);
    }
}

Note: The if condition above is necessary only if you want to iterate over the properties which are the dictionary object's very own. Because for..in will iterate through all the inherited enumerable properties.

Or

Object.keys(dictionary).forEach(function(key) {
    console.log(key, dictionary[key]);
});
Answer from thefourtheye on Stack Overflow
Top answer
1 of 11
1043

tl;dr

  1. In ECMAScript 2017, just call Object.entries(yourObj).
  2. In ECMAScript 2015, it is possible with Maps.
  3. In ECMAScript 5, it is not possible.

ECMAScript 2017

ECMAScript 2017 introduced a new Object.entries function. You can use this to iterate the object as you wanted.

'use strict';

const object = {'a': 1, 'b': 2, 'c' : 3};

for (const [key, value] of Object.entries(object)) {
  console.log(key, value);
}

Output

a 1
b 2
c 3

ECMAScript 2015

In ECMAScript 2015, there is not Object.entries but you can use Map objects instead and iterate over them with Map.prototype.entries. Quoting the example from that page,

var myMap = new Map();
myMap.set("0", "foo");
myMap.set(1, "bar");
myMap.set({}, "baz");

var mapIter = myMap.entries();

console.log(mapIter.next().value); // ["0", "foo"]
console.log(mapIter.next().value); // [1, "bar"]
console.log(mapIter.next().value); // [Object, "baz"]

Or iterate with for..of, like this

'use strict';

var myMap = new Map();
myMap.set("0", "foo");
myMap.set(1, "bar");
myMap.set({}, "baz");

for (const entry of myMap.entries()) {
  console.log(entry);
}

Output

[ '0', 'foo' ]
[ 1, 'bar' ]
[ {}, 'baz' ]

Or

for (const [key, value] of myMap.entries()) {
  console.log(key, value);
}

Output

0 foo
1 bar
{} baz

ECMAScript 5:

No, it's not possible with objects.

You should either iterate with for..in, or Object.keys, like this

for (var key in dictionary) {
    // check if the property/key is defined in the object itself, not in parent
    if (dictionary.hasOwnProperty(key)) {           
        console.log(key, dictionary[key]);
    }
}

Note: The if condition above is necessary only if you want to iterate over the properties which are the dictionary object's very own. Because for..in will iterate through all the inherited enumerable properties.

Or

Object.keys(dictionary).forEach(function(key) {
    console.log(key, dictionary[key]);
});
2 of 11
159

Try this:

dict = {0:{1:'a'}, 1:{2:'b'}, 2:{3:'c'}}
for (var key in dict){
  console.log( key, dict[key] );
}

0 Object { 1="a"}
1 Object { 2="b"}
2 Object { 3="c"}
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › entries
Object.entries() - JavaScript - MDN Web Docs
Using array destructuring, you can iterate through objects easily. ... // Using for...of loop const obj = { a: 5, b: 7, c: 9 }; for (const [key, value] of Object.entries(obj)) { console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" } // Using array methods Object.entries(obj).forEach(([key, ...
Discussions

javascript - Iterate through object properties - Stack Overflow
In the first iteration of the loop, propt would be "name". ... Save this answer. ... Show activity on this post. Objects in JavaScript are collections of properties and can therefore be looped in a for each statement. You should think of obj as an key value collection. More on stackoverflow.com
🌐 stackoverflow.com
Loop through array and create object of specific key/value pairs?
I stuck your whole post in chat GPT, lol: You can use a loop to iterate over the array of objects and create a new object with the required key-value pairs. Here's an example of how you could do it: // input array const slots = [ {"name": "Name1", "task": "Task1"}, {"Value": "Current Value", "temperature": 67, "status": "on"}, {"mode": "Current Mode", "status": "selected"} ]; // output object const slotsObj = {}; // iterate over the input array for (let i = 0; i < slots.length; i++) { const slot = slots[i]; // check if the current object has the required properties if (slot.hasOwnProperty("name")) { slotsObj.name = slot.name; } else if (slot.hasOwnProperty("Value")) { slotsObj.value = slot.Value; } else if (slot.hasOwnProperty("mode")) { slotsObj.mode = slot.mode; } } // create the final object with the required properties const result = { slotsObj }; console.log(result); // output: { slotsObj: { name: 'Name1', value: 'Current Value', mode: 'Current Mode' } } This code checks each object in the slots array for the required properties (name, Value, and mode) and adds them to the slotsObj object. Then it creates the final object with the required structure. Note that if an object doesn't have one of the required properties, it will simply be ignored. More on reddit.com
🌐 r/learnjavascript
6
1
March 31, 2023
How would I access the first key-value pair of the "files" object using a forEach loop?
You can use Object.values() or a similar method, or a for...in loop More on reddit.com
🌐 r/learnjavascript
7
2
November 24, 2022
For...in vs Object.keys
It's largely preference or depending on whether or not you're dealing with a more imperative or more functional codebase. But you should at least be aware of the differences that go beyond style. Specifically, for..in will capture inherited keys whereas Object.keys sticks only to own properties. For example: var par = { prop1 : "some val" }; var obj = Object.create(par); obj.prop2 = "some other val"; for(key in obj){ console.log("Key: ", key) console.log("Value: ", obj[key]) } // ^ prop1 and prop2 Object.keys(obj).forEach((key)=>{ console.log("For Each Key: ", key) console.log("For Each Value: ", obj[key]) }) // ^ prop2 only More on reddit.com
🌐 r/javascript
24
14
April 24, 2018
People also ask

How can I iterate over an object entries while preserving the order of keys?
Objects in JavaScript are unordered by default, and the order of keys is not guaranteed. If you need to iterate over an object's properties in a specific order, you have a few options: - Use the Object.entries() method to get an array of \[key, value\] pairs, sort the array based on the keys, and then iterate over the sorted array. - Create an array of keys in the desired order and then access the corresponding values from the object using those keys. - Use a Map instead of an object, as Map maintains the insertion order of key-value pairs. You can convert an object to a Map using new M
🌐
latenode.com
latenode.com › home › blog › how to iterate over a javascript object?
How to Iterate Over a JavaScript Object? - Latenode Blog
Are there any other libraries or methods for object iteration?
Yes, there are several other libraries and methods that provide utilities for object iteration. Some popular ones include: - Underscore.js: Provides the _.each() method for iterating over objects. - Lodash: Offers various methods like _.forOwn(), _.forIn(), and _.forEach() for object iteration. - jQuery: Has the $.each() method that can be used for iterating over objects. These libraries offer additional functionality and can be helpful if you are already using them in your project. However, if you only need basic object iteration, the native JavaScript methods discussed in this articl
🌐
latenode.com
latenode.com › home › blog › how to iterate over a javascript object?
How to Iterate Over a JavaScript Object? - Latenode Blog
Can I use arrow functions with these iteration methods?
Yes, you can use arrow functions with methods like map(), forEach(), and _.forOwn(). Arrow functions provide a concise syntax for writing function expressions. For example: ``` Object.entries(person).map(([key, value]) =&gt; {  console.log(key + ': ' + value); }); ``` However, keep in mind that arrow functions have a lexical this binding, which means they inherit the this value from the surrounding scope. If you need to access the this context within the iteration callback, you may need to use a regular function expression instead.
🌐
latenode.com
latenode.com › home › blog › how to iterate over a javascript object?
How to Iterate Over a JavaScript Object? - Latenode Blog
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-iterate-over-a-javascript-object
Iterate over a JavaScript object - GeeksforGeeks
Object.entries(exampleObj).map(entry ... returns an array of keys of the object and forEach() method is an array method that allows you to iterate over each element in the array....
Published   October 14, 2025
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › for...in
for...in - JavaScript | MDN - MDN Web Docs
The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.
Top answer
1 of 16
2608

Iterating over properties requires this additional hasOwnProperty check:

for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
        // do stuff
    }
}

It's necessary because an object's prototype contains additional properties for the object which are technically part of the object. These additional properties are inherited from the base object class, but are still properties of obj.

hasOwnProperty simply checks to see if this is a property specific to this class, and not one inherited from the base class.


It's also possible to call hasOwnProperty through the object itself:

if (obj.hasOwnProperty(prop)) {
    // do stuff
}

But this will fail if the object has an unrelated field with the same name:

var obj = { foo: 42, hasOwnProperty: 'lol' };
obj.hasOwnProperty('foo');  // TypeError: hasOwnProperty is not a function

That's why it's safer to call it through Object.prototype instead:

var obj = { foo: 42, hasOwnProperty: 'lol' };
Object.prototype.hasOwnProperty.call(obj, 'foo');  // true
2 of 16
1346

As of JavaScript 1.8.5 you can use Object.keys(obj) to get an Array of properties defined on the object itself (the ones that return true for obj.hasOwnProperty(key)).

Object.keys(obj).forEach(function(key,index) {
    // key: the name of the object key
    // index: the ordinal position of the key within the object 
});

This is better (and more readable) than using a for-in loop.

Its supported on these browsers:

  • Firefox (Gecko): 4 (2.0)
  • Chrome: 5
  • Internet Explorer: 9

See the Mozilla Developer Network Object.keys()'s reference for futher information.

🌐
Hostman
hostman.com › tutorials › looping-through-objects-s-keys-and-values-in-javascript
How to loop through objects keys and values in Javascript? | Hostman
The for loop feature for iterating ... of working with objects and shorten your code. The built-in Object.keys() method in JavaScript allows you to get an array of all the keys of a given object....
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › how-to-iterate-over-objects-in-javascript
Loop Through an Object in JavaScript – How to Iterate Over an Object in JS
November 7, 2024 - const population = { male: 4, female: ... ${population[key]}`); } } To avoid the stress and difficulty of looping and to use the hasOwnProperty method, ES6 and ES8 introduced object static methods....
🌐
Latenode
latenode.com › home › blog › how to iterate over a javascript object?
How to Iterate Over a JavaScript Object? - Latenode Blog
June 11, 2026 - The for...in loop is a traditional way to iterate over an object's properties. It allows you to access both the keys and values of an object.
🌐
Flexiple
flexiple.com › javascript › loop-through-object-javascript
How to loop through objects keys and values in Javascript?
For more specific tasks, Object.keys(), Object.values(), and Object.entries() methods can be utilized, offering arrays of keys, values, or [key, value] pairs, respectively. These arrays can then be looped over using array iteration methods like ...
🌐
Inspirnathan
inspirnathan.com › posts › 140-iterating-through-objects-in-javascript
Iterating Through Keys and Values of Objects in JavaScript
July 21, 2022 - Once we have an array, we can iterate through them using a for loop, for...of loop, or Array.forEach. I'm going to go ahead and use Array.forEach since it seems like cleanest approach. ... const obj = { pizzas: 1, donuts: 2, potatoes: 3 }; Object.keys(obj).forEach(key => { console.log(key); }) /* OUTPUT: pizzas donuts potatoes */ We can use the Object.values method to return an array containing all values of an object.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › foreach-object
Iterating Through an Object with `forEach()` - Mastering JS
You can then iterate over each key in the object using forEach(). const obj = { name: 'Jean-Luc Picard', rank: 'Captain' }; // Prints "name Jean-Luc Picard" followed by "rank Captain" Object.keys(obj).forEach(key => { console.log(key, obj[key]); ...
🌐
Coderwall
coderwall.com › p › _kakfa › javascript-iterate-through-object-keys-and-values
JavaScript iterate through object keys and values (Example)
June 26, 2023 - This approach of looping through keys and values in an object can be used to perform more useful operations on the object, for instance the method could call a function passed in on each of the values. An example of this is in the foIn method in mout.js which iterates through the object keys and values calling the function passed in.
🌐
Medium
sunnygandhi01.medium.com › how-to-iterate-through-an-object-keys-and-values-in-javascript-d8bb464615fa
How to Iterate through an object keys and values in JavaScript. | by Sunnygandhi | Medium
January 13, 2021 - const fruits : { Mango: 100, Apple: 10, Orange: 50, Kiwi: 5 };const entries = Object.entries(fruits); console.log(entries);Output: ['Mango', '100']; ['Apple', '10']; ['Orange', '50']; ['Kiwi', '5'];//To iterate over the array return we can use for..each method. Object.entries(fruits).forEach(function(key, index) { console.log(`${key}: ${value}`) };Output: Mango: 100 Apple: 10 Orange: 50 Kiwi: 5 ... That’s all for iterating over object properties in JavaScript.
🌐
DEV Community
dev.to › fpaghar › object-iteration-ways-in-javascript-pej
Object Iteration ways in JavaScript - DEV Community
April 8, 2024 - It's a convenient way to iterate over object properties when you're only interested in keys. const obj = { a: 1, b: 2, c: 3 }; Object.keys(obj).forEach(key => { console.log(key + ': ' + obj[key]); }); Remember that Object.keys() ignores inherited ...
🌐
Attacomsian
attacomsian.com › blog › javascript-iterate-objects
How to iterate over object keys and values in JavaScript
November 12, 2022 - The simplest and most popular way to iterate over an object’s keys and values is using the for...in loop: const birds = { owl: '?', eagle: '?', duck: '?', chicken: '?' } for (const key in birds) { console.log(`${key} -> ${birds[key]}`) } // ...
🌐
Futurestud.io
futurestud.io › tutorials › iterate-through-an-objects-keys-and-values-in-javascript-or-node-js
Iterate Through an Object’s Keys and Values in JavaScript or Node.js
March 3, 2022 - The for…in loop exists in JavaScript for a long time. It was already supported in Internet Explorer 6. If you need to support old browsers, the for…in loop is a solid choice. This loop gives you access to each key in a given object. You can then access the related value using index access. Here’s an example iterating over an object and printing each key-value pair:
🌐
JavaScript Tutorial
javascripttutorial.net › home › iterate object in javascript
Iterate Object in JavaScript
March 29, 2020 - let person = { firstName: 'John', lastName: 'Doe', age: 25, ssn: '123-456-2356' }; Object.keys(person).forEach(key => { console.log(`${key}: ${person[key]}`); });Code language: JavaScript (javascript) ... The Object.values() takes an object as an argument and returns an array of the object’s values.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › iterate-object
How to Iterate Over a JavaScript Object - Mastering JS
For a POJO, this distinction doesn't matter. But when you use inheritance, this distinction can be important. Using a for/in loop lets you iterate over all an object's keys, including inherited keys.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
How to Iterate Over Object Keys With JavaScript | Envato Tuts+
May 20, 2022 - This method iterates over all of the object's enumerable, non-symbol properties. In the following example, we use it to iterate over all three properties of obj, and for each property, we log a string consisting of the property name (i.e. its key) and its corresponding value.
🌐
Effective TypeScript
effectivetypescript.com › 2020 › 05 › 26 › iterate-objects
Effective TypeScript › Item 54: Know How to Iterate Over Objects
May 26, 2020 - Hopefully this doesn't happen in ... If you want to iterate over the keys and values in an object, use either a keyof declaration (let k: keyof T) or Object.entries....