What is the best method for looping through one object and all of its properties, then moving onto another object and so on...
At the moment, my code only loops through lvlTitle. Whereas I would like to loop through lvlTitle then move on to statement1, statement2, and statement3 and then to the next object.
Thank you.
Here is the javascript:
// Different Levels
var lvls = {
start: {
lvlTitle: 'Lets Start!',
},
lvl1: {
lvlTitle: 'Drinks/Soda/water',
statement1: 'lvl1 info...',
statement2: 'lvl1 more info...',
statement3: 'lvl1 more more info...'
},
lvl2: {
lvlTitle: 'Portion Control/Meals',
statement1: 'lvl2 info...',
statement2: 'lvl2 more info...',
statement3: 'lvl2 more more info...'
},
lvl3: {
lvlTitle: 'Salt',
statement1: 'lvl3 info...',
statement2: 'lvl3 more info...',
statement3: 'lvl3 more more info...'
}
}
// Array of all the levels
var allLvls = [];
// Populate users array
for (var key in lvls) {
allLvls.push(lvls[key]);
}
console.log(allLvls[0].lvlTitle);
let myIndex = 1;
let printText = document.querySelector('.text-container');
printText.innerHTML = allLvls[0].lvlTitle;
function nextLevel() {
printText.innerHTML = allLvls[myIndex].lvlTitle;
myIndex = (myIndex + 1) % (allLvls.length);
};
printText.addEventListener('click', nextLevel);
And here is the Html:
<div class="full-page">
<div class="click-container">
<ul class="text-container">
<li class="text-content">
<div></div>
</li>
</ul>
</div>
Also here is a jsFiddle: Iterate through Array List of Objects
Drinks/Soda/waterthenlvl1 info...and so on?