iterate on object js code example
Example 1: iterate object javascript
let obj = {
key1: "value1",
key2: "value2",
key3: "value3",
key4: "value4",
}
Object.entries(obj).forEach(([key, value]) => {
console.log(key, value);
});
Example 2: javascript loop object
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
Object.keys(obj).forEach(key => {
console.log(key, obj[key]);
});
for (let key in obj) {
let value = obj[key];
console.log(key, value);
}
Example 3: javascript loop through object example
var person={
first_name:"johnny",
last_name: "johnson",
phone:"703-3424-1111"
};
for (var property in person) {
console.log(property,":",person[property]);
}
Example 4: foreach object javascript
const games = {
"Fifa": "232",
"Minecraft": "476"
"Call of Duty": "182"
};
Object.keys(games).forEach((item, index, array) => {
let msg = `There is a game called ${item} and it has sold ${games[item]} million copies.`;
console.log(msg);
});
Example 5: how to iterate object inside object in javascript
let obj = {
key1: "value1",
key2: "value2",
key3: "value3"
}
for (let key in obj) {
let value = obj[key];
console.log(key, value);
}
Example 6: foreach object javascript
const obj = {
a: "aa",
b: "bb",
c: "cc",
};
for(let key in obj) {
console.log(key);
console.log(obj[key]);
}