xxxxxxxxxx
const myObj = {user1: {username: "alvin", id: 1}, user2: {username: "david", id: 2}}
for(let key in myObj){
console.log(key, myObj[key])
}
// output
// user1 {username: "alvin",id: 1}
// user2 {username: "alvin",id: 1}
xxxxxxxxxx
var obj = { first: "John", last: "Doe" };
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
xxxxxxxxxx
for (var property in object) {
if (object.hasOwnProperty(property)) {
// Do things here
}
}
xxxxxxxxxx
const obj = { a: 1, b: 2 };
Object.keys(obj).forEach(key => {
console.log("key: ", key);
console.log("Value: ", obj[key]);
} );
xxxxxxxxxx
var person={
first_name:"johnny",
last_name: "johnson",
phone:"703-3424-1111"
};
for (var property in person) {
console.log(property,":",person[property]);
}
xxxxxxxxxx
Object.entries(obj).forEach(
([key, value]) => console.log(key, value)
);
xxxxxxxxxx
var p = {
"p1": "value1",
"p2": "value2",
"p3": "value3"
};
// for-in
for (var key in p) {
if (p.hasOwnProperty(key)) {
console.log(key + " -> " + p[key]);
}
}
// for-of with Object.keys()
for (var key of Object.keys(p)) {
console.log(key + " -> " + p[key])
}
// Object.entries()
for (let [key, value] of Object.entries(p)) {
console.log(`${key}: ${value}`);
}
xxxxxxxxxx
// Looping through arrays created from Object.keys
const keys = Object.keys(fruits)
for (const key of keys) {
console.log(key)
}
// Results:
// apple
// orange
// pear
xxxxxxxxxx
const obj = { a: 1, b: 2, c: 3 }
for (const [key, value] of Object.entries(obj)) {
console.log(key, value)
}
xxxxxxxxxx
var obj = {
first: "John",
last: "Doe"
};
//
// Visit non-inherited enumerable keys
//
Object.keys(obj).forEach(function(key) {
console.log(key, obj[key]);
});
xxxxxxxxxx
var person = {"name":"Rasel", age:26};
for (var property in person) {
console.log(person[property]);
}