Q:

object foreach

const students = {
  adam: {age: 20},
  kevin: {age: 22},
};

Object.entries(students).forEach(student => {
  // key: student[0]
  // value: student[1]
  console.log(`Student: ${student[0]} is ${student[1].age} years old`);
});
/* Output:
Student: adam is 20 years old
Student: kevin is 22 years old
*/
6
const obj = {
  name: 'Jean-Luc Picard',
  rank: 'Captain'
};

// Prints "name Jean-Luc Picard" followed by "rank Captain"
Object.entries(obj).forEach(entry => {
  const [key, value] = entry;
  console.log(key, value);
});
2
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
1
const obj = {
  name: 'Jean-Luc Picard',
  rank: 'Captain'
};

// Prints "name Jean-Luc Picard" followed by "rank Captain"
Object.keys(obj).forEach(key => {
  console.log(key, obj[key]);
});
0
Add thisvar p = {
    "p1": "value1",
    "p2": "value2",
    "p3": "value3"
};

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}
2
for (var key in validation_messages) {
    // skip loop if the property is from prototype
    if (!validation_messages.hasOwnProperty(key)) continue;

    var obj = validation_messages[key];
    for (var prop in obj) {
        // skip loop if the property is from prototype
        if (!obj.hasOwnProperty(prop)) continue;

        // your code
        alert(prop + " = " + obj[prop]);
    }
}
1

New to Communities?

Join the community