How can I access only two elements of this array


#1

I want to access the question nd answe
I tried with
console.log(allQuestion[question][answers]);

Var allQuestion = [
{
question: “Who is the president of America”,
answers: [“Obama”, “Trump”, “Clinton” ],
correctAnswer: “Kendrick”
}
]


#2

are you sure you posted correct code?

there is a { on line 2:

Var allQuestion = [
{ // this one here
question: “Who is the president of America”,
answers: [“Obama”, “Trump”, “Clinton” ],
correctAnswer: “Kendrick”
]

but it has no matching } to close it, without being sure about what you used (arrays and objects) its really difficult to say how to access it


#3

Yeah. Thanks. I made the corrections.
I only want to access the two elements [question] and [answers]


#4

allQuestion is an array, so first we have access by index to access the object.

so you get allQuestion[0], then we can use the property name (full stop notation) to get the value, or if you want to use the associative array notation (square brackets) the key should be of type string

you will need to two look-ups, we can only access one key at a time, you can’t retrieve question and answers in a single line of code


#5

Thank @stetim94 i’m able to fix it out with

console.log(allQuestions[index].question, allQuestions[index].anwsers);

and this the output.
im now tryin to print them on them in a form of a radio buttom with the answer .

Do you have any idea out


#6

then you have to abandon the console and focus on webpage. Sure, its possible. What have you got so far?


#7

@stetim94 im stuck for days now to print on the web


#8

What have you got so far?


#9
`
<html>
    <body>

        <div id="question">HipHop Quiz</div>
        <button type="button" onclick="render()">New Quiz</button>
        <div id="showQuiz"></div>
        
        <script>
           }
                 const allQuestions = [
                    {
                        question : "Which of them is from compton", 
                        anwsers: ["Nas", "Tupac", "Omarion", "Kendick"], 
                        correctAnswer: "Kendrick"
                    },
                    {
                        question: "founder of HipHop",
                        anwsers: ["Tupac", "Eazy E", "Kendick", "Bambata"],
                        correctAnswer: "Bambata"
                    },
                    {    question: "Who won BET Hip Hop Album 2018", 
                        anwsers: ["Kendrick", "Bruno", "Jay Z", "Drake"], 
                        correctAnswer: "Kendrick",
                    },
                    {
                        question: "Best Female HipHop Art 2018",
                        anwsers: ["Azelia", "Nicki", "Cardi_b", "Mama Rap" ],
                        correctAnswer: "Nicki"
                    }
                ]
                     function render() {
                            var index, unique;
                            var print = '<ul>';
                            for (i = 0; i < allQuestions.length; i++) {
                                index = Math.floor(Math.random() * allQuestions.length);
                          
                                
                            }
                            
                        var questionHTML = "<P>" + allQuestions[question][0] + "</P>";        

                        console.log(allQuestions[index].question, allQuestions[index].anwsers);
                            unique = allQuestions.splice(index);
                            allQuestions.push(unique[allQuestions]);
                        
                            //document.getElementById('showQuiz').innerHTML = 'Answer all ';
                            
                            console.log(unique);
                    }
                    
                    document.getElementById('showQuiz').innerHTML = render();
    
            </script>      
    </body>
</html>
`

#10
<script>
           } // what is this } doing here?

What is the purpose of your for loop?

here:

var questionHTML = "<P>" + allQuestions[question][0] + "</P>";

allQuestions is an array, so you will need to access by index first:

allQuestions[0]

this will give you access to the object. here you can access properties of object:

allQuestions[0].question

or if you prefer to use square brackets:

allQuestions[0]["question"]

but for this to work, the key (question) needs to be a string.


#11

Okay …
The function of the loop is to randomize the question in the array.

Over there i want to load the $question and $answer only in the browser…

with this
allQuestions[0][question]


#12

that is currently not happening, what help do you need from me with this? Currently your loop generates random indexes several times, only remembering the last value you assign to index variable

like i try to explain in my previous answer, allQuestions[0][question] won’t work. You can access the question by property:

allQuestions[0].question

or by key:

allQuestions[0]["question"]

but you can’t combine property and key like you did:

allQuestions[0][question] // question is undefined

#13

Thanks Sir
I want to loop throuhgh the question without repeatin and print the question and answers on the browser…

I lacking oo much now


#14

Did you figure it out now?


#15

Not yet many trials an yet nop but I won’t stop till I get it.

will need more guidelines.

You are great Sir


#16

What do you need guidance with?

Running someone through a trail and error campaign is exhausting, what is it you need explained?


#17

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.