I am beginning a exercise to create a quiz in javascript. Anyway, I have an array which contains the questions; each question is a anonymous object...
var allQuestions = [{
"question": "Who was Luke's wingman in the battle at Hoth?",
"choices": ["Dak", "Biggs", "Wedge", "fx-7"],
"correctAnswer": 0},
{
"question": "What is the registry of the Starship Reliant?",
"choices": [ "NX-01", "NCC-1864", "NCC-1701", "NCC-2000"],
"correctAnswer": 1}...etc.
At this point i'd like to simply iterate through them and insert them into the DOM.
var output = '';
for (key in allQuestions[0]) {
output += '<li>' + allQuestions[0] + '</li>';
}
var update = document.getElementById("question");
update.innerHTML = output;
But all I get is:
[object Object]
[object Object]
[object Object]
At some point i'd like to match or have 'question', 'choices' flow in to corresponding elements on the page....
<h2>question</h2> //question from object
<ul id="question">
<li>choice</li> //choice from object
<li>choice</li>
<li>choice</li>
<li>choice</li>
allQuestions[0]multiple times – and since that is an object, you are only getting[object Object]when that is put into a string context …allQuestions[0].key.