Object nesting
Creating nested objects
Some more complex data structures can be created by nesting lists and objects inside other objects.
js
const user = {
name: 'Jane Doe',
email: 'jane.doe@example.com',
address: {
street: '123 Main St',
city: 'Sometown',
county: 'Anyshire',
postcode: 'AB1 2CD'
},
borrowedBooks: [
{
id: 1,
title: "The Handmaid's Tale",
author: 'Margaret Atwood',
year: 1985,
isbn: '9780099740919'
}
]
}Accessing nested objects
We can chain together the property names to access the nested object's properties.
js
console.log(user.address.street)console
123 Main StAccessing nested arrays
Similarly, for nested arrays we can write:
js
console.log(author.books[0].title) // 'The Handmaid's Tale'console
The Handmaid's TaleOptional chaining
This is a nice syntax for checking if a property exists before trying to access it.
js
function logFirstBookTitle(user) {
return user.borrowedBooks[0]?.title
}
Corndel