Using a for…of
const people = [
{
name: "Bob",
occupation: "Software Engineer"
},
{
name: "Kim",
occupation: "Architect"
}
]
console.log("Using for...of:")
for(const [index, person] of people.entries()) {
console.log(`${person.occupation} at index ${index}`)
}
/*
Using for...of:
Software Engineer at index 0
Architect at index 1
*/
Using a forEach
const people = [
{
name: "Bob",
occupation: "Software Engineer"
},
{
name: "Kim",
occupation: "Architect"
}
]
console.log("Using forEach:")
people.forEach((person, index) => {
console.log(`${person.occupation} at index ${index}`)
})
/*
Using forEach:
Software Engineer at index 0
Architect at index 1
*/
Like this:
Like Loading...
Related