#Mapping Data from API

1 messages · Page 1 of 1 (latest)

granite sundial
#

I am working on Quiz App ,when I mapped it just returns all the questions from API.
what should i do to get one question at a time?

gritty sandal
#

You're getting there. So data.results is an array of objects. If you console.log(data.results[0]) you will see the first object and within that object you'll see the question, correct answer, and incorrect answers. When I started this project I just fetched two questions. You're placing the array of questions info into a state variable. Next you can map over that state (array of questions) to work with each question separately. You might call a <Question /> component... setting a unique key and sending props info you need to display the question and multiple-choice answers.

granite sundial
#

Thanks man ! i will try that

#

when i console.log(data.results.question) to check it says undefied

gritty sandal
#

try console.log(data.results[0].question) ... to reference the question within the first object in the array of "questions" objects, i.e. each object within the array contains info for a question.

granite sundial
#

Thanks you for your response bro , I successfully rendered questions to UI but how can I render my Choices? its like correct_answer : "something" and rest are stored as incorrect_answers : "","",""

#

here it is

gritty sandal
#

combine correct answer and incorrect answers... then sort them (you decide how) so which choice is the correct answer is not obvious, i.e. always the first choice. Then you can list the choices as buttons.

wary needle
river temple
wary needle