I have an array of object and I'm trying to create HTML elements for each object and for each object's property, but got stuck, when I try to loop through containerDiv and appending object's properties as children elements.
- I want to create a div for each object with the class name of containerDiv;
- I want to loop through containerDiv;
- I want to create an element for each object property and append them as children elements to containerDiv;
My code sample looks like this:
var parent = document.getElementById('parent');
function createHTMLElements() {
for(var i = 0; i < arrayOfObjects.length; i++) {
var containerDiv = document.createElement('div');
containerDiv.className = 'container';
parent.appendChild(containerDiv);
// Loop through containerDiv and append object properties as child elements
}
}
var arrayOfObjects = [
{
name: 'John',
surname: 'Doe'
},
{
name: 'David',
surname: 'Mills'
}
]