Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To extract key-value pairs from an array of objects that require multiple lines, you can use a loop and access the specific key and value using bracket notation. For example:

let arr = [
  {
    name: 'John Doe',
    age: 30,
    address: {
      street: '123 Main St',
      city: 'Anytown',
      state: 'CA'
    }
  },
  {
    name: 'Jane Smith',
    age: 25,
    address: {
      street: '456 Elm St',
      city: 'Another Town',
      state: 'NY'
    }
  }
];

for (let i = 0; i < arr.length; i++) {
  let name = arr[i]['name'];
  let age = arr[i]['age'];
  let street = arr[i]['address']['street'];
  let city = arr[i]['address']['city'];
  let state = arr[i]['address']['state'];
  console.log(name, age, street, city, state);
}

This code will iterate through the array of objects and extract the name, age, street, city, and state values using bracket notation to access the nested address object. The values can then be used for whatever purpose is needed.