1 | initial version |
To retrieve an item from an array within an object, you can use dot notation and bracket notation.
let obj = {
fruits: ["apple", "banana", "orange"]
};
// Access the fruits array:
console.log(obj.fruits); // ["apple", "banana", "orange"]
let obj = {
fruits: ["apple", "banana", "orange"]
};
// Access the second item (banana) in the fruits array:
console.log(obj.fruits[1]); // "banana"
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"