0

I am having trouble maping through an array, i am using Object.entries to map the data, find below the example of the json structure. here is what i have tried but i get errors, i know the bath is not correct.

{
  Object.entries(this.props.optionData.steps).map((t, k) => (
    <option onClick={(e) => this.optionSelectHandle(t, k)} key={k} value={t[0]}>
      {t[1]}
    </option>
  ));
}


{
    "steps": [{
            "step_num": 1,
            "description": "Description"
        },
        {
            "step_num": 2,
            "description": "Description",
            "uncommon_field": "some data"
        }
    ]
}
2
  • 2
    Why entries when its an array? Commented May 23, 2022 at 20:30
  • 2
    Object.entries is only for objects. It returns an array you can map over. However, when you've already got an array, you can simply map over it immediately. Commented May 23, 2022 at 20:47

1 Answer 1

3

you could simply use the map function on steps like such:

{
  this.props.optionData.steps.map(step => (
    <option onClick={(e) => this.optionSelectHandle(e)} key={step.step_num} value={step.step_num}>
      {step.description}
    </option>
  ));
}

as it is already an array

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.