iterate object javascript
let obj = {
key1: "value1",
key2: "value2",
key3: "value3",
key4: "value4",
}
Object.entries(obj).forEach(([key, value]) => {
console.log(key, value);
});
iterate object javascript
let obj = {
key1: "value1",
key2: "value2",
key3: "value3",
key4: "value4",
}
Object.entries(obj).forEach(([key, value]) => {
console.log(key, value);
});
foreach object javascript
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
*/
javascript loop through object
for (var property in object) {
if (object.hasOwnProperty(property)) {
// Do things here
}
}
object foreach
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]);
});
js loop through object
const obj = { a: 1, b: 2 };
Object.keys(obj).forEach(key => {
console.log("key: ", key);
console.log("Value: ", obj[key]);
} );
javascript foreach object
const list = {
key: "value",
name: "lauren",
email: "[email protected]",
age: 30
};
// Object.keys returns an array of the keys
// for the object passed in as an argument.
Object.keys(list).forEach(val => {
let key = val;
let value = list[val];
console.log(`${key} : ${value}`);
});
// Returns:
// "key : value"
// "name : lauren";
// "email : [email protected]"
// "age : 30"
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us