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}
1const obj = { a: 1, b: 2 };
2
3Object.keys(obj).forEach(key => {
4 console.log("key: ", key);
5 console.log("Value: ", obj[key]);
6} );
1var person={
2 first_name:"johnny",
3 last_name: "johnson",
4 phone:"703-3424-1111"
5};
6for (var property in person) {
7 console.log(property,":",person[property]);
8}
1for (var prop in obj) {
2 if (Object.prototype.hasOwnProperty.call(obj, prop)) {
3 // do stuff
4 }
5}