iterate an object code example

Example 1: loop an object properties in ts

Object.keys(obj).forEach(e => console.log(`key=${e}  value=${obj[e]}`));

Example 2: javascript iterate over object keys and values

const object1 = {
  a: 'somestring',
  b: 42
};

for (let [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}

// expected output:
// "a: somestring"
// "b: 42"
// order is not guaranteed

Example 3: how to iterate object inside object in javascript

let obj = {
  key1: "value1",
  key2: "value2",
  key3: "value3"
}
for (let key in obj) {
  let value = obj[key];
  console.log(key, value);
}
// key1 value1
// key2 value2
// key3 value3

Example 4: javascript loop through object properties

for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
        // do stuff
    }
}

Example 5: going through every attributes of an object javascript

let a = {x: 200, y: 1}
let attributes = Object.keys(a)
console.log(attributes)
//output: ["x", "y"]