0

If I have an array of objects that also has nested arrays, how do I get/destructure the sub array? For instance, what if I want to create a new array with only address object?

const people = [
  {
    name: "Jane",
    address: {
      street: "123 Main St",
      city: "Broadway"
    }
  },
  {
    name: "John",
    address: {
      street: "123 Other St",
      city: "Manhattan"
    }
  }
]
Eric Nguyen
  • 926
  • 3
  • 15
  • 37

1 Answers1

0

Please try this

const people = [
  {
    name: "Jane",
    address: {
      street: "123 Main St",
      city: "Broadway"
    }
  },
  {
    name: "John",
    address: {
      street: "123 Other St",
      city: "Manhattan"
    }
  }
]

const address=people.map((item)=>item.address)
console.log(address);
kelvin kantaria
  • 1,438
  • 11
  • 15