-1

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] });
  }
9
  • totally new to this world :) Commented Jul 3, 2020 at 11:23
  • Does this help ? stackoverflow.com/questions/52954908/… Commented Jul 3, 2020 at 11:23
  • describe the problem. your expected input and output. This question should be useful for future readers who are searching for similar problems, and should be in a format that a person with no prior knowledge of your code can read and understand the problem you are having. Commented Jul 3, 2020 at 11:25
  • 'this.state.questions[key] is not iterable' i want to push optiontext from input field to options array Commented Jul 3, 2020 at 11:26
  • You probably want to add an option to the questions array using the push() prototype method. It would be good practice to explore the other prototype methods shown on this page. Commented Jul 3, 2020 at 11:27

1 Answer 1

0

extract option and append a value. Clone questions before splicing in new question with merged options. Update state.

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 options = this.state.questions[key].options; //trying to access array in array of object 
  options = [...options, addText];
  const questions = [...this.state.questions];
  questions.splice(key, 1, {...questions[key], options});

  this.setState({
    questions
  });
}
<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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.