for (var k in target){
    if (target.hasOwnProperty(k)) {
         alert("Key is " + k + ", value is " + target[k]);
    }
}

hasOwnProperty is used to check if your target really has that property, rather than having inherited it from its prototype. A bit simpler would be:

for (var k in target){
    if (typeof target[k] !== 'function') {
         alert("Key is " + k + ", value is" + target[k]);
    }
}

It just checks that k is not a method (as if target is array you'll get a lot of methods alerted, e.g. indexOf, push, pop,etc.)

Answer from J0HN on Stack Overflow
🌐
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); });
Discussions

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
Is Object.keys(obj).forEach really any better than for-in loop?
Yes, there is a difference. Object.keys() iterates over "own" properties, while for-in iterates over all enumerable properties, even those inherited in the prototype. Also, speed really isn't a concern for nearly all use cases. More on reddit.com
🌐 r/javascript
35
13
February 1, 2017
How do i remove elements from a list while in a foreach loop?
Instead of using a foreach loop, use a for loop and iterate backwards. That way you're removing objects "behind" you in the iterator, and you won't run into index problems. More on reddit.com
🌐 r/Unity3D
34
5
March 3, 2023
Loop through an object inside of the render method?

Map over the keys of the object using Object.keys():

{Object.keys(yourObject).map(function(key) {
    return <div>Key: {key}, Value: {yourObject[key]}</div>;
})}
More on reddit.com
🌐 r/reactjs
3
1
April 17, 2015
🌐
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 ·
🌐
Medium
medium.com › @louistrinh › iterating-through-javascript-objects-and-arrays-for-foreach-and-beyond-e9e6ce5376d3
Iterating Through JavaScript Objects and Arrays: for, forEach, and Beyond | by Louis Trinh | Medium
May 10, 2024 - Object.entries(person).forEach(([key, value]) => { console.log(key, value); // Output: name Alice, age 30 }); Use Object.keys() to create an array of keys and iterate over them: Object.keys(person).forEach(key => { console.log(key); // Output: ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Map › forEach
Map.prototype.forEach() - JavaScript - MDN Web Docs
function logMapElements(value, key, map) { console.log(`m[${key}] = ${value}`); } new Map([ ["foo", 3], ["bar", {}], ["baz", undefined], ]).forEach(logMapElements); // Expected output: "m[foo] = 3" // Expected output: "m[bar] = [object Object]" // Expected output: "m[baz] = undefined"
🌐
Atomizedobjects
atomizedobjects.com › blog › javascript › how-to-get-the-javascript-foreach-key-value-pairs-from-an-object
How to get the JavaScript forEach key value pairs from an object | Atomized Objects
In the above example we are using Object.entries to first convert the object into an array of key value pairs. We then use forEach with the array of key value pairs so that we can iterate through each of them.
Find elsewhere
🌐
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

🌐
Softwareshorts
softwareshorts.com › how-to-get-the-javascript-foreach-key-value-pairs-from-an-object
How to get the JavaScript forEach key value pairs from an object | SoftwareShorts
For example if you have an object containing a few trees where the keys are the names of the trees and the values are the number of trees you have and you pass it into Object.keys then it will return an array of the names of all the trees.
🌐
Built In
builtin.com › articles › javascript-loop-through-associative-arrays
JavaScript Loop Through Associative Arrays Guide | Built In
As you can see, there are several ways we can loop through the key-value pair of an associative array object with JavaScript. You can loop through an associative array in JavaScript with three methods: for-in loop, object.entries with the forEach method and for-of loop.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › foreach-object
Iterating Through an Object with `forEach()` - Mastering JS
May 29, 2020 - 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]); }); The Object.values() function returns an array of the object's own enumerable property values.
🌐
DEV Community
dev.to › ljnce › foreach-object-values-object-keys-3n1f
forEach( ): Object.values / Object.keys - DEV Community
August 22, 2020 - Object.keys(array).forEach(function(key) { console.log(key); //--> 0 1 }); Object.values · Object.values(array).forEach(function(value) { console.log(value); //--> name: 'John' name: 'Mary' console.log(value.name); //--> John Mary }); .forEach · array.forEach(function (val) { console.log(val); //--> name: 'John' name: 'Mary' }) Subscribe ·
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › foreach
How to Use forEach() in JavaScript - Mastering JS
December 16, 2020 - If you want to use forEach() with ... is not a function" arguments.forEach(val => console.log(val)); } test(); const map = new Map([['key', 'value']]); ......
🌐
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 - const tutorials = { nodejs: 123, android: 87, java: 14, json: 7 } const keys = Object.keys(tutorials) // ['nodejs', 'android', 'java', 'json'] You can combine Object.keys with array methods to loop over each key . You may also access the related value for the given key using the index access on the original object: Object.keys(tutorials).forEach(key => { console.log(`${key}: ${tutorials[value]}`) }) // nodejs: 123 // android: 87 // java: 14 // json: 7 ·
🌐
Sabe
sabe.io › blog › javascript-foreach-key-value-object
How to use forEach with Key Value Pairs Object in JavaScript | Sabe
October 21, 2022 - JAVASCRIPTconst object = { name: "Sabe.io", url: "https://sabe.io" }; Object.keys(object).forEach(key => { console.log(object[key]); }); ... JAVASCRIPTconst object = { name: "Sabe.io", url: "https://sabe.io" }; Object.values(object).forEach(value => { console.log(value); });
🌐
C# Corner
c-sharpcorner.com › article › how-we-can-use-foreach-with-maps-and-sets
How we can use forEach with Map and Set In JavaScript
April 25, 2023 - However, you can use the forEach method of the Map object to iterate over the entries of the Map. Here's an example. const myMap = new Map([['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]); myMap.forEach((value, key) => { ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-map-foreach-method
JavaScript Map forEach() Method - GeeksforGeeks
July 15, 2025 - JavaScript Map.forEach method is used to loop over the map with the given function and executes the given function over each key-value pair.
🌐
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 - You can then use array looping methods, to iterate through the array and retrieve the value of each property. const fruits : { Mango: 100, Apple: 10, Orange: 50, Kiwi: 5 };// convert object to key's array const keys = Object.keys(fruits);//console.log() all keys of the object fruits. console.log(keys); //Output: ['Mango, 'Apple', 'Orange', 'Kiwi']//Now we can iterate over the fruit object. keys.forEach(key, index) { console.log(`${key}: ${fruits[key]}`); });Output: Mango: 100 Apple: 10 Orange: 50 Kiwi: 5 ·
🌐
Inspirnathan
inspirnathan.com › posts › 140-iterating-through-objects-in-javascript
Iterating Through Keys and Values of Objects in JavaScript
July 21, 2022 - const obj = { pizzas: 1, donuts: 2, potatoes: 3 }; Object.values(obj).forEach(value => { console.log(value); }) /* OUTPUT: 1 2 3 */ We can use the Object.entries method to return an array of [key, value] pairs. Each pair is an array.
🌐
W3Schools
w3schools.com › JSREF › jsref_forEach.asp
JavaScript Array forEach() Method
JS Arrays · Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean ·