Skip to content

Instantly share code, notes, and snippets.

@scriptschat
Created Sep 22, 2020
Embed
What would you like to do?
Using for in construct to iterate over an object data structure`
const person = {
firstName: 'Virat',
lastName: 'Kohli',
email: 'vk@rcb.in',
profession: 'Cricketer',
team: 'India/RCB'
}
for(attr in person) {
console.log(`Attribute: ${attr} Value: ${person[attr]}`);
}
// Output
// Attribute: firstName Value: Virat
// Attribute: lastName Value: Kohli
// Attribute: email Value: vk@rcb.in
// Attribute: profession Value: Cricketer
// Attribute: team Value: India/RCB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment