Ask Your Question
0

What is the method to extract key-value pairs from an array of objects that require multiple lines?

asked 2022-08-21 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-01-28 21:00:00 +0000

woof gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-08-21 11:00:00 +0000

Seen: 19 times

Last updated: Jan 28 '23