js object loop array get value by key code example

Example 1: loop through object javascript

for (var property in object) {
  if (object.hasOwnProperty(property)) {
    // Do things here
  }
}

Example 2: foreach object javascript

const obj = {
  a: "aa",
  b: "bb",
  c: "cc",
};
//This for loop will loop through all keys in the object.
// You can get the value by calling the key on the object with "[]"
for(let key in obj) {
  console.log(key);
  console.log(obj[key]);
}

//This will return the following:
// a
// aa
// b
// bb
// c
// cc