I have a file named AnimalsData that has an object that consists of an array. The array consists of an object and another array. It looks like this:
var animals = {
animalClass : [
{
className : "Reptiles",
animalArray : [
{
image1 : "https://upload.wikimedia.org/wikipedia/commons/thumb/7/76/Coast_Garter_Snake.jpg/500px-Coast_Garter_Snake.jpg",
image2 : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/Nerodia_sipedon_shedding.JPG/440px-Nerodia_sipedon_shedding.JPG",
name : "Snake",
description :
}, "blah, blah, blah"
{
image1 : "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f9/Crocodilia_collage.jpg/600px-Crocodilia_collage.jpg",
image2 : "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Caiman_crocodilus_Tropicario_2.JPG/440px-Caiman_crocodilus_Tropicario_2.JPG",
name : "Crocodilia",
description : "blah, blah, blah"
},
{
image1 : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/Bartagame_fcm.jpg/500px-Bartagame_fcm.jpg",
image2 : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/Lizard_in_Yemen.JPG/440px-Lizard_in_Yemen.JPG",
name : "Lizard",
description : "blah, blah, blah"
},
{
image1 : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/86/Chelonia_mydas_is_going_for_the_air.jpg/440px-Chelonia_mydas_is_going_for_the_air.jpg",
image2 : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1d/Turtle3m.JPG/500px-Turtle3m.JPG",
name : "Turtle",
description : "blah, blah, blah"
},
]
},
.......etc., etc
My HTML file looks like this:
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Animal Photo Album</title>
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<!--<script src="js/handlebars-v3.0.3.js"></script>-->
<script src="js/AnimalsData.js"></script>
<script src="js/assignment.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/myjavascript.css" rel="stylesheet">
</head>
<body>
<main id="content" class="container">
</main>
</body>
I'm trying to get the values of the className object from the animalClass array. I have been using the following two stackoverflow pages as a guide:
Accessing Objects in JSON Array and Access / process (nested) objects, arrays or JSON
I have another javascript file which has the following:
EDIT I changed "name" in the for in loop and the jQuery statement to className in order to reduce the confusion.
function executeObject() {
for(var i = 0; i < animals.animalClass.length; i++) {
for(var className in animals.animalClass[i].className) {
$('<p>' + ClassName + '</p>').insertAfter('#content');
}
}
};
$(function(){
executeObject();
});
However, this only produces numbers. I've tried various tweaks to the above but they either end up being undefined or the insert "className" and "animalArray" three times. The examples looked pretty straight forward but I'm obviously missing something.