1Object.entries(obj).forEach(
2 ([key, value]) => console.log(key, value)
3);
1// object to loop through
2let obj = { first: "John", last: "Doe" };
3
4// loop through object and log each key and value pair
5//ECMAScript 5
6Object.keys(obj).forEach(function(key) {
7 console.log(key, obj[key]);
8});
9
10//ECMAScript 6
11for (const key of Object.keys(obj)) {
12 console.log(key, obj[key]);
13}
14
15//ECMAScript 8
16Object.entries(obj).forEach(
17 ([key, value]) => console.log(key, value)
18);
19
20// OUTPUT
21/*
22 first John
23 last Doe
24*/
1const obj = { a: 1, b: 2 };
2
3Object.keys(obj).forEach(key => {
4 console.log("key: ", key);
5 console.log("Value: ", obj[key]);
6} );
1const object = { a: 1, b: 2, c: 3 };
2
3for (const property in object) {
4 console.log(`${property}: ${object[property]}`);
5}
1const obj = { a: 1, b: 2, c: 3 }
2
3for (const [key, value] of Object.entries(obj)) {
4 console.log(key, value)
5}
1for (var key in validation_messages) {
2 // skip loop if the property is from prototype
3 if (!validation_messages.hasOwnProperty(key)) continue;
4
5 var obj = validation_messages[key];
6 for (var prop in obj) {
7 // skip loop if the property is from prototype
8 if (!obj.hasOwnProperty(prop)) continue;
9
10 // your code
11 alert(prop + " = " + obj[prop]);
12 }
13}