5

I am trying to print data inside a nested map function. For some reason it does not print anything. here is what I have:

  {dataFormat.protein_questions.map((item, index) => (
        <div key={_.uniqueId()} className="item">
         <div className="inline fields">
           <Field
              onChange={handleChange}
              name={`protein_question[${index}].units_of_measurement`}
              component="select"
              className="ui dropdown2"
              required>

                 {item.typing_methods.map((method, methodIndex) => {
                    method.unitsOfMeasurement.map((unit, unitIndex) => (
                       <option value={unit.title}>{unit.title}</option>
                    ));
                 })}

          </Field>
         </div>
       </div>
  ))}

The options are not printing any data inside the select for some reason. When I console log the data inside the nested loop it shows fine. Anyone know why this is happening?

By the way this is inside my render function in a JSX view file.

9
  • but you are not returing anything from the item.typeing_methods.map, so I am not sure what you are expecting Commented Oct 12, 2017 at 9:16
  • I think you need to add return statement every where Commented Oct 12, 2017 at 9:20
  • 1
    @DhavalRajani no, he is doing it correctly in the other map statements, not that they start with ( and not {, the first will implicitely return the statement, the second expects a return Commented Oct 12, 2017 at 9:22
  • Just out of curiosity, was it one of you two who voted to close this question? Commented Oct 12, 2017 at 9:26
  • @user3574492 yeah, I did, as for me this is a offtopic as a it is a small I oversight on your part Commented Oct 12, 2017 at 17:18

1 Answer 1

9

It looks like you forgot return before method.unitsOfMeasurement...:

{item.typing_methods.map((method, methodIndex) => {
  return method.unitsOfMeasurement.map((unit, unitIndex) => (
    <option value={unit.title}>{unit.title}</option>
  ));
})}
Sign up to request clarification or add additional context in comments.

1 Comment

Genius! I forgot you had to always return something in the main loop. I'll accept when it lets me.

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.