+ 1

I don't know how to access an object value inside an array. How do I print the value "cat"?

const ourPets = [ { animalType: "cat", names: [ "Meowzer", "Fluffy", "Kit-Cat" ] } ]

12th Aug 2022, 4:44 PM
Umar Akorede
Umar Akorede - avatar
4 Réponses
+ 3
//You can get and print the value of animalType this way: console.log(ourPets[0].animalType); //or: console.log(ourPets[0]["animalType"]); //to get the value from a array, just add the index you need at the end. console.log(ourPets[0].names[0]); console.log(ourPets[0]["names"][0]);
12th Aug 2022, 4:46 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 2
I've edited my previous comment. Now it should work
12th Aug 2022, 4:55 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
+ 1
It says error
12th Aug 2022, 4:52 PM
Umar Akorede
Umar Akorede - avatar
+ 1
Worked perfectly! Thanks pal
12th Aug 2022, 5:03 PM
Umar Akorede
Umar Akorede - avatar