I want to take input in these format from user
"questions":[
{"question": "Trial question","options":[{"A":"hi","B":"hi","C":"hi","D":"hi"}], "ans":"A","marks":5},
{"question": "Trial question2","options":[{"A":"hi","B":"hi","C":"hi","D":"hi"}], "ans":"A","marks":1},
{"question": "Trial question2","options":[{"A":"hi","B":"hi","C":"hi","D":"hi"}], "ans":"A","marks":2}
]
Here the array size is dynamic, user can input n number of this structure.(Here size is 3)
I am trying this way to take the input
constructor() {
super();
this.state = {
name: "",
subject: "",
numQuestion: 0,
totalNumQuestion: "",
questions: [
{
question: "",
options: [
{
A: "",
B: "",
C: "",
D: "",
},
],
marks: "",
ans: "",
},
],
};
this.handleSubmit = this.handleSubmit.bind(this);
}
Here I am taking inputs in following way
for (let i = 1; i <= this.state.numQuestion; i++) {
inputs.push(
<input
placeholder="Question name"
name={`question`}
onChange={this.onChange}
required
/>,
<br />,
<input
placeholder="Option A"
name={`A`}
onChange={this.onChange}
required
/>,
<input
placeholder="Option B"
name={`B`}
onChange={this.onChange}
required
/>,
<input
placeholder="Option C"
name={`C`}
onChange={this.onChange}
required
/>,
<input
placeholder="Option D"
name={`D`}
onChange={this.onChange}
required
/>,
<br />,
<input
placeholder="Answer"
name={`ans`}
onChange={this.onChange}
required
/>,
<br />,
<input
placeholder="Marks"
name={`marks`}
onChange={this.onChange}
required
/>,
<br />
);
This is not working. How can i modify the code to achieve the data in structure specified above