Introduction
Object literals in JavaScript are key, value pairs. The value could be:
- string
- an object
- an array
- a number
- a function
- an object literal
- a set
- a map
Looping/iterating through an object
An example object:
const user = {
name: 'Naftali Murgor',
age: 29,
hobbies: ['swimming', 'hiking', 'writing'],
country: 'Kenya',
}
To iterate through the properties we use for-property-in-object
:
for (const property in user) {
// prints all "property": "value"
console.log(`${property}: ${user[property]}`)
}
// outputs:
// name: Naftali Murgor
// age: 29
// hobbies: swimming, hiking,writing
// country: Kenya
Follow me on twitter @nkmurgor where I tweet about interesting topics.