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

上面的代码如果键是名称则尝试显示数据,然后创建optgroup标签,否则它将显示选项值中的所有数据。之后,应该在选项值内的选项值 id<Option key= {key} >{singleData[key]}</Option>和要显示的文本就是该值。有人知道怎么做吗 :( ?

4

1 回答 1

1

对于this.props.datacatcreate an 的每个条目OptGroup,然后使用该条目的所有选项填充它,除了带有 key 的选项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>
));

演示

于 2019-11-01T02:15:30.800 回答