I'm new to ReactJS library, and I'm trying to print the following structure of array:
Main Array[
Array0[
Object0{
questions: "question1",
answer1: "answer1",
answer2: "answer2"
},
Object1{
questions: "question1",
answer1: "answer1",
answer2: "answer2"
}
]
]
This structure of array is hold in state called question, I have tried to create new functional component and print it on user screen but I received the following error:
TypeError: quest[0].map is not a function
My target is to print Object0 and Object1 data.
const [question, setQuestion] = useState([]);
setQuestion([
[
{
questions: "question1",
answer1: "answer1",
answer2: "answer2"
},
{
questions: "question2",
answer1: "answer1",
answer2: "answer2"
}
]
]);
//Component
const QuestionsComponent = function questions(){
return (
<div>
{
question.map(function (quest){
quest[0].map(function(ques){
return quest.questions;
})
})
}
</div>
);
}
return(
<>
<QuestionsComponent />
</>
);
What is the correct approach to print array of objects inside of array?
setQuestion()to theuseState(). Also you probably meant to passquestionas a prop toQuestionsComponent