I am using a for loop to display a list of items on my page. As you can see, each object is declared in my array.
const animals = [
{
name: "monkey",
food: "fruit",
img: "images/fruit.jpg",
},
{
name: "horse",
food: "hay",
img: "images/hay.jpg",
},
{
name: "sealion",
food: "fish",
img: "images/fish.jpg",
}
];
new Vue ({
el: '#app',
data: {
zoo: animals
}
});
The below code would print a list of animals and the food they like on a listings page.
<ul>
<li v-for="(item, index) in zoo">
<p>{{index }} {{ item.name }}</p>
<p>{{index }} {{ item.food }}</p>
</li>
</ul>
However, I also need to use the information stored in this array elsewhere on my site. But, this time not in a loop.
For a separate details page, I would need the information for only the third animal (index position 2)
<h2>My favorite animal is a {{ item[2].name }} and it eats {{ item[2].food }} </h2>
Is there a way to do this?
My favorite animal is a {{ zoo[2].name }} and it eats {{ zoo[2].food }}?