Use index notation with the key.

Object.keys(obj).forEach(function(k){
    console.log(k + ' - ' + obj[k]);
});
Answer from Josiah Keller on Stack Overflow
Discussions

How do I loop through or enumerate a JavaScript object? - Stack Overflow
In Underscore.js you can find method _.each(), which iterates over a list of elements, yielding each in turn to a supplied function (pay attention to the order of arguments in iteratee function!): _.each(obj, function(value, key) { console.log(key, value); }); Lo-Dash provides several methods for iterating over object properties. Basic _.forEach... More on stackoverflow.com
🌐 stackoverflow.com
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
Obtaining key, value from $.each - json OBJECT; NOT array
🌐 forum.jquery.com
Loop and get key/value pair for JSON array using jQuery
I'm looking to loop through a JSON array and display the key and value. It should be a simplified version of the following post, but I don't seem to have the syntax correct: jQuery 'each' loop w... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-iterate-json-object-in-javascript
How to Iterate JSON Object in JavaScript? - GeeksforGeeks
July 23, 2025 - Inside the loop, we can access ... GeeksforGeeks contact: +91-9876543210 city: Noida · Object.keys() can be used to get an array of the keys from a JSON object....
🌐
ZetCode
zetcode.com › javascript › jsonforeach
JavaScript JSON forEach - Iterating Over JSON Arrays
Object.entries(obj).forEach(([key, value]) => { console.log(`${key} ${value}`); }); We go over the entries of each object and print the key and the value to the console. id 1 main.js:12:13 first_name Robert main.js:12:13 last_name Schwartz main.js:12:13 email rob23@gmail.com main.js:12:13 ...
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript foreach json key-value | example code
JavaScript foreach JSON key value | Example code
January 27, 2023 - <!DOCTYPE html> <html> <body> <script> var data = { "VERSION": "1001.10", "JOBNAME": "EXEC_", "JOBHOST": "Loal", "LSFQUEUE": "80", "LSFLIMIT": "2022-10-27", "NEWUSER": "3" }; Object.keys(data).forEach(function(key) { console.log('Key : ' + key + ', Value : ' + data[key]) }) </script> </body> </html> ... Do comment if you have any doubts or suggestions on this JS JSON topic.
Top answer
1 of 16
5192

You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn't come from the prototype.

Here is the snippet:

var p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}

For-of with Object.keys() alternative:

var p = {
    0: "value1",
    "b": "value2",
    key: "value3"
};

for (var key of Object.keys(p)) {
    console.log(key + " -> " + p[key])
}

Notice the use of for-of instead of for-in, if not used it will return undefined on named properties, and Object.keys() ensures the use of only the object's own properties without the whole prototype-chain properties

Using the new Object.entries() method:

Note: This method is not supported natively by Internet Explorer. You may consider using a Polyfill for older browsers.

const p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (const [key, value] of Object.entries(p)) {
  console.log(`{value}`);
}
2 of 16
1383

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" };

Object.keys(obj).forEach(function(key) {
    console.log(key, obj[key]);
});

ECMAScript 6 adds for...of:

for (const key of Object.keys(obj)) {
    console.log(key, obj[key]);
}

ECMAScript 8 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach(
    ([key, value]) => console.log(key, value)
);

You can combine for...of, destructuring, and Object.entries:

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

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

🌐
Tabnine
tabnine.com › home page › code › java › javax.json.jsonobject
javax.json.JsonObject.forEach java code examples | Tabnine
source.containsKey(key)) { builder.add(path + '/' + key, value); } }); } ... JsonObjectBuilder builder = Json.createObjectBuilder(); s.forEach((key, value) -> { if (t.containsKey(key)) { t.forEach((key, value) -> { if (!
🌐
Medium
allaboutcode.medium.com › top-3-ways-to-loop-through-a-json-object-in-javascript-67ca21d33a24
Top 3 Ways to Loop Through a JSON Object in JavaScript | by Marika Lam | Medium
September 27, 2022 - This returns an array of the key/value pairs of the object we pass it: const res = JSON.parse(xhr.responseText);Object.entries(res).forEach((entry) => { const [key, value] = entry; console.log(`${key}: ${value}`); });// id: SvzIBAQS0Dd // joke: What did the pirate say on his 80th birthday?
Find elsewhere
🌐
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 - Object.entries(tutorials).forEach(([key, value]) => { console.log(`${key}: ${value}`) }) // nodejs: 123 // android: 87 // java: 14 // json: 7
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › foreach-key-value
How to Use forEach() with Key Value Pairs - Mastering JS
July 14, 2021 - 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]); }); const obj = { name: 'Jean-Luc Picard', rank: 'Captain' }; // Prints "Jean-Luc Picard" followed by "Captain" Object.values(obj).forEach(val => { console.log(val); });
🌐
Reddit
reddit.com › r/learnjavascript › how would i access the first key-value pair of the "files" object using a foreach loop?
r/learnjavascript on Reddit: How would I access the first key-value pair of the "files" object using a forEach loop?
November 24, 2022 -

Here's how the JSON looks

"files": {
            "file1": {
                "filename": "file1",
                "timestamp": 1073692
            }
}

It's basically a value that is contained by every object in the array and I need to access the filename key that's inside every object.

Hope this makes sense

edit: the object key matches the file name every time

🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Use of .Object in this forEach loop example - JavaScript
January 24, 2019 - json.forEach(function(val) { var keys = Object.keys(val); html += "<div class = 'cat'>"; keys.forEach(function(key) { html += "<strong>" + key + "</strong> " + val[key] + "</br>;"; }); html += "</div><br>";
🌐
SitePoint
sitepoint.com › blog › javascript › how to loop through a json response in javascript
How to Loop Through a JSON Response in JavaScript — SitePoint
February 15, 2024 - This returns an array of the key/value pairs of the object we pass it: const res = JSON.parse(xhr.responseText); Object.entries(res).forEach((entry) => { const [key, value] = entry; console.log(`${key}: ${value}`); }); // id: SvzIBAQS0Dd // joke: What did the pirate say on his 80th birthday?
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › entries
Object.entries() - JavaScript - MDN Web Docs
js · // 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, value]) => { console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" }); Polyfill of Object.entries in core-js ·
🌐
Coderwall
coderwall.com › p › _kakfa › javascript-iterate-through-object-keys-and-values
JavaScript iterate through object keys and values (Example)
June 26, 2023 - It could be useful to use this approach to create an array of all the keys in an object and pass that back, or we could pass in a function to a method like this which iterates through the keys and values, and calls the function for specific values. ... There’s also Object.keys in Node.js and modern browsers. See MDN for details. ... if (!Object.keys) Object.keys = function(o) { if (o !== Object(o)) throw new TypeError('Object.keys called on a non-object'); var k = [], p; for (p in o) if (Object.prototype.hasOwnProperty.call(o, p)) k.push(p); return k; } ... ObjectKit.forEach = function Object_forEach (object, callback) { for (var key in object) { if (object.hasOwnProperty(key)) callback(key, object[key]); } };
🌐
IQCode
iqcode.com › code › javascript › c-json-foreach-key-value
c# json foreach key value Code Example
February 25, 2022 - var dict = JObject.Parse(@"{""1"":""Name 1"",""2"":""Name 2""}"); var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json); foreach(var kv in dict) { Console.WriteLine(kv.Key + ":" + kv.Value); }
🌐
Microverse
microverse.org › home › blog › how to loop through the array of json objects in javascript
How to Loop Through the Array of JSON Objects in JavaScript
September 29, 2022 - {% code-block language="js" %} var json = { jsonData: [ {one: [11, 12, 13, 14, 15]}, {two: [21, 22, 23]}, {three: [31, 32]} ] }; for (var i=0; i<json.jsonData.length; i++) { for (var key in json.jsonData[i]) { for (var j= 0; j<json.jsonData[i][key].length; j++) { console.log(json.jsonData[i][key][j]) } } } ‍{% code-block-end %} Now let’s have a look at different types of Loops and their uses. We shall discuss the “While” Loop, the “Do While Loop” and the “The ForEach() Loop”.‍ · A While Loop also doesn’t have a counter and only runs if the specified condition is true. Here’s an example demonstrating the while loop where the loop runs till infinity: {% code-block language="js" %} var infiniteValue = true; while (infiniteValue) { console.log("Infinite") } ‍{% code-block-end %}