Home > Back-end >  Can't use Object.entries with array index structure in ReactJs
Can't use Object.entries with array index structure in ReactJs

Time:05-24

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"
        }
    ]
}

CodePudding user response:

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

  • Related