loop on object javascript keys and values code example
Example: how to iterate through a js object
let object = {
x: 10,
y: 10,
z: 10
};
let keys = Object.keys(object);
// now 3 different ways:
// method 1:
key.forEach(function(key){
let attribute = object[key];
// do stuff
}
);
//method 2:
for(let key of keys){
let attribute = object[key];
// do stuff
}
//method 3:
for(let i = 0; i < keys.length; i++){
let key = keys[i];
let attribute = object[key];
// do stuff
}