ReactJs 在 optgroup 中显示数据

ReactJs display data in optgroup

this.props.datacat.map(singleData=>{
  return Object.keys(singleData).map((key,i) =>{
     if(key==='name'){
        return <OptGroup label  = {singleData[key]}>
     }else{
        return <Option key= {key} >{singleData[key]}</Option> 
     }
        return </OptGroup>
  })
})

(this.props.datacat)

的数据
[
  {
    "4967": "Others.",
    "4968": "Sports & Beachwear > Others.",
    "4969": "Lingerie & Nightwear > Others.",
    "4971": "Pants & Shorts > Others.",
    "name": "Women Clothes"
  },
  {
    "4798": "Supplements > Others.",
    "4802": "Men's Grooming > Others.",
    "4952": "Others.",
    "4953": "Medical Supplies > Others.",
    "4954": "Personal Pleasure > Others.",
    "4955": "Personal Care > Others.",
    "4956": "Pedicure & Manicure > Others.",
    "6647": "Lips > Lip Tint"
    "name": "Health & Beauty"
  }
]

上面的代码试图显示数据,如果键是 name 然后创建 optgroup 标签,否则它将显示所有期权价值中的数据。之后,选项值 id<Option key= {key} >{singleData[key]}</Option> 应该在选项值内,要显示的文本就是该值。任何人都知道如何做到这一点:( ?

this.props.datacat 的每个条目创建一个 OptGroup,然后用该条目的所有选项填充它,但键为 name 的选项除外:

this.props.datacat.map((singleData) => (
  <OptGroup label={singleData["name"]}>
    {Object.keys(singleData)
      .filter(key => key !== "name")
      .map(key => (
        <Option key={key}>{singleData[key]}</Option>
      ))}
  </OptGroup>
));

Demo