I get the error: this.state.questions[key] is not iterable
I want to push optiontext from input field to options array
this.state = {
sections: [{
key: 1,
value: 1
}],
optionText: '',
questions: [{
id: Math.round(Math.random() * 1000000000000000),
query: '',
sectionId: 1,
selectedType: 1,
options: ['1st option', ],
isChecked: true
}],
}
addOption = (key) => {
const addtext = this.state.optionText //typed text in input
let option = this.state.questions[key].options; //trying to access array in array of object
this.setState({
questions: [...this.state.questions[key], addtext]
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
sections: [{ key: 1, value: 1 }],
optionText: '',
surveyTitle: '',
description: '',
questions: [{
id: Math.round(Math.random() * 1000000000000000),
query: '',
sectionId: 1,
selectedType: 1,
options: ['1st option',],
isChecked: true
}],
}
addOption = (key) => {
const addtext = this.state.optionText //typed text in input
let option = this.state.questions[key].options; //trying to access array in array of object
this.setState({ questions: [...this.state.questions[key], addtext] });
}
questionsarray using thepush()prototype method. It would be good practice to explore the other prototype methods shown on this page.