Ask Your Question

Revision history [back]

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"