javascript pt literate over object code example
Example 1: javascript loop through object
for (var property in object) {
if (object.hasOwnProperty(property)) {
}
}
Example 2: js loop through object
const obj = { a: 1, b: 2 };
Object.keys(obj).forEach(key => {
console.log("key: ", key);
console.log("Value: ", obj[key]);
} );
Example 3: loop key in object
const fruits = { apple: 28, orange: 17 }
for(key in fruits){
console.log(key)
}
Example 4: how to iterate through a js object
let object = {
x: 10,
y: 10,
z: 10
};
let keys = Object.keys(object);
key.forEach(function(key){
let attribute = object[key];
}
);
for(let key of keys){
let attribute = object[key];
}
for(let i = 0; i < keys.length; i++){
let key = keys[i];
let attribute = object[key];
}