Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Working with Nested and Dynamic Objects | Objects and Type Conversion
JavaScript Data Types Foundations

bookWorking with Nested and Dynamic Objects

12345678910111213141516171819202122232425
const user = { name: "Alice", contact: { email: "alice@example.com", address: { city: "Wonderland", zip: "12345" } } }; // Accessing a nested property using dot notation console.log(user.contact.email); // "alice@example.com" // Modifying a nested property using dot notation user.contact.address.city = "Looking Glass"; console.log(user.contact.address.city); // "Looking Glass" // Accessing a nested property using bracket notation console.log(user["contact"]["address"]["zip"]); // "12345" // Adding a new nested property dynamically const key = "phone"; user.contact[key] = "555-1234"; console.log(user.contact.phone); // "555-1234"
copy

When working with objects in JavaScript, you often encounter properties that are themselves objects, resulting in nested objects. To access or modify these nested properties, you can use either dot notation or bracket notation. Dot notation is straightforward and concise, as in user.contact.email, and is the preferred choice when you know the exact property names ahead of time and those names are valid identifiers (no spaces or special characters).

Bracket notation, such as user["contact"]["address"]["zip"], allows you to use variables as property keys or access properties with names that include spaces, special characters, or are dynamically determined at runtime. This is especially useful when you need to add or access properties whose names are not fixed, as shown when adding a new phone property using a variable key.

In summary:

  • Use dot notation for direct, known property names that are valid identifiers;
  • Use bracket notation when property names are dynamic, stored in variables, or contain characters not allowed in identifiers.
question mark

Which statement about accessing and modifying nested object properties is correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 6.25

bookWorking with Nested and Dynamic Objects

Swipe um das Menü anzuzeigen

12345678910111213141516171819202122232425
const user = { name: "Alice", contact: { email: "alice@example.com", address: { city: "Wonderland", zip: "12345" } } }; // Accessing a nested property using dot notation console.log(user.contact.email); // "alice@example.com" // Modifying a nested property using dot notation user.contact.address.city = "Looking Glass"; console.log(user.contact.address.city); // "Looking Glass" // Accessing a nested property using bracket notation console.log(user["contact"]["address"]["zip"]); // "12345" // Adding a new nested property dynamically const key = "phone"; user.contact[key] = "555-1234"; console.log(user.contact.phone); // "555-1234"
copy

When working with objects in JavaScript, you often encounter properties that are themselves objects, resulting in nested objects. To access or modify these nested properties, you can use either dot notation or bracket notation. Dot notation is straightforward and concise, as in user.contact.email, and is the preferred choice when you know the exact property names ahead of time and those names are valid identifiers (no spaces or special characters).

Bracket notation, such as user["contact"]["address"]["zip"], allows you to use variables as property keys or access properties with names that include spaces, special characters, or are dynamically determined at runtime. This is especially useful when you need to add or access properties whose names are not fixed, as shown when adding a new phone property using a variable key.

In summary:

  • Use dot notation for direct, known property names that are valid identifiers;
  • Use bracket notation when property names are dynamic, stored in variables, or contain characters not allowed in identifiers.
question mark

Which statement about accessing and modifying nested object properties is correct?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
some-alt