65. Working with Nested Structures
Objects and Arrays can hold other objects and arrays, creating complex data structures. This is how real-world data (like API responses) is structured.
Example: Nested Object
javascript const company = { name: 'Tech Solutions', address: { street: '123 Main St', city: 'New York' }, employees: 50 };
Accessing Nested Data
Chain the dot notation to drill down into the object structure.
javascript let city = company.address.city; // New York console.log(city);
Example: Array of Objects
javascript const posts = [ { id: 1, text: 'Post 1', tags: ['js', 'web'] }, { id: 2, text: 'Post 2', tags: ['css'] } ];
Accessing Array and Object Data Together
Use array brackets for the index, and dot notation for object properties.
javascript // Access the 'text' of the second post (index 1) let postText = posts[1].text; // Post 2
// Access the first tag of the first post let firstTag = posts[0].tags[0]; // js