1let obj = {
2 key1: "value1",
3 key2: "value2",
4 key3: "value3"
5}
6
7Object.keys(obj).forEach(key => {
8 console.log(key, obj[key]);
9});
10// key1 value1
11// key2 value2
12// key3 value3
13
14// using for in - same output as above
15for (let key in obj) {
16 let value = obj[key];
17 console.log(key, value);
18}
1var obj = { first: "John", last: "Doe" };
2
3Object.keys(obj).forEach(function(key) {
4 console.log(key, obj[key]);
5});
1for (var property in object) {
2 if (object.hasOwnProperty(property)) {
3 // Do things here
4 }
5}
6
1const obj = { a: 1, b: 2 };
2
3Object.keys(obj).forEach(key => {
4 console.log("key: ", key);
5 console.log("Value: ", obj[key]);
6} );
1// an easy way is to
2
3const obj = {1: 'a', 2: 'b', 3: 'c',};
4
5for (const i in obj)
6 console.log(obj[i]);
7
8//it is a simple way to loop on values of an object