+ 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" ] } ]
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]);
+ 2
I've edited my previous comment. Now it should work
+ 1
It says error
+ 1
Worked perfectly! Thanks pal