Ask Your Question
1

What is the process of retrieving an item from an array that is nested within an object?

asked 2023-05-09 14:02:15 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-09 14:28:01 +0000

huitzilopochtli gravatar image

To retrieve an item from an array within an object, you can use dot notation and bracket notation.

  1. To access the array within the object, use dot notation with the property name that contains the array. For example:
let obj = {
  fruits: ["apple", "banana", "orange"]
};

// Access the fruits array:
console.log(obj.fruits); // ["apple", "banana", "orange"]
  1. To access a specific item within the array, use bracket notation with the index of the item you want to retrieve. For example:
let obj = {
  fruits: ["apple", "banana", "orange"]
};

// Access the second item (banana) in the fruits array:
console.log(obj.fruits[1]); // "banana"
  1. If the array is nested within another object property, you can chain the dot notation to access it. For example:
let obj = {
  grocery: {
    fruits: ["apple", "banana", "orange"]
  }
};

// Access the fruits array within the grocery object:
console.log(obj.grocery.fruits); // ["apple", "banana", "orange"]

// Access the third item (orange) in the fruits array:
console.log(obj.grocery.fruits[2]); // "orange"
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: 2023-05-09 14:02:15 +0000

Seen: 21 times

Last updated: May 09 '23