iterating through an object in javascript code example
Example 1: javascript loop through object
Object.entries(obj).forEach(
([key, value]) => console.log(key, value)
);
Example 2: object loop in javascript
const object = { a: 1, b: 2, c: 3 };
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
Example 3: javascript iterate over object keys and values
const object1 = {
a: 'somestring',
b: 42
};
for (let [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
Example 4: loop through object javascript
const fruits = {
apple: 28,
orange: 17,
pear: 54,
}
const keys = Object.keys(fruits)
console.log(keys)
Example 5: 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];
}
Example 6: iterate over object javascript
Object.keys(pokemons).forEach(console.log);