Read values inside an object inside another object.

Nested Objects

nested_objects.js
const city = "Austin";
const userName = ({ user: { name: "Mina", profile: { city: city } } }).user.name;
const homeCity = ({ user: { name: "Mina", profile: { city: city } } }).user.profile.city;
const badge = userName + "@" + homeCity;

console.log("user=" + userName);
console.log("city=" + homeCity);
console.log("badge=" + badge);

Walk Into the Shape

  1. The outer object has a user object.
  2. user.name is Mina.
  3. user.profile.city is Austin.
  4. The badge combines both values as Mina@Austin.
user -> name -> Mina
user -> profile -> city -> Austin
nested-objects Nested objects model structured data. Each dot moves one level deeper into the shape.

Exercise: nested_objects.js

Read a nested user name and city, then print a name-at-city badge