为了账号安全,请及时绑定邮箱和手机立即绑定

如何循环数组并将对象数组中的值输出为“值”和“标签”?

如何循环数组并将对象数组中的值输出为“值”和“标签”?

qq_笑_17 2021-05-07 14:12:01
我在单独的文件中将以下数据作为对象数组export const usersRowData = [  {    "id": 1,    "name": "Leanne Graham",    "username": "Bret",    "email": "Sincere@april.biz",    "address": {      "street": "Kulas Light",      "suite": "Apt. 556",      "city": "Gwenborough",      "zipcode": "92998-3874",      "geo": {        "lat": "-37.3159",        "lng": "81.1496"      }    },    "value": "Leanne Graham",    "label": "Leanne Graham"  },  {    "id": 2,    "name": "Ervin Howell",    "username": "Antonette",    "email": "Shanna@melissa.tv",    "address": {      "street": "Victor Plains",      "suite": "Suite 879",      "city": "Wisokyburgh",      "zipcode": "90566-7771",      "geo": {        "lat": "-43.9509",        "lng": "-34.4618"      }    }  }]在我的React组件内部,我将这些过滤器数据用于下拉列表硬编码为值和标签:const filters = [  {    label: "name",    options: [      { value: "Dietrich", label: "Dietrich" },      { value: "Patricia", label: "Patricia" }    ]  },  {    label: "username",    options: [      { value: "Kamren", label: "Kamren" },      { value: "Bret", label: "Bret" }    ]  },  {    label: "email",    options: [{ value: "Sincere@april.biz", label: "Sincere@april.biz" }]  }];我如何循环数组将每个名称输出为我的react组件内的值和标签,以便可以在列表中显示它们。
查看完整描述

2 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

您是说要显示选项和价值吗?


const filters = [

  {

    label: "name",

    options: [

      { value: "Dietrich", label: "Dietrich" },

      { value: "Patricia", label: "Patricia" }

    ]

  },

  {

    label: "username",

    options: [

      { value: "Kamren", label: "Kamren" },

      { value: "Bret", label: "Bret" }

    ]

  },

  {

    label: "email",

    options: [{ value: "Sincere@april.biz", label: "Sincere@april.biz" }]

  }

];


function App() {

  return (

    <section>

    {filters.map(data => {

      const label = <label> {data.label} </label>

      return (

        <section>

            {label}

          <select>

            {data.options.map(option => {

              return(

                <option value={option.value}> {option.value} </option>

              )

            })}

          </select>

        </section>

      )

    })}

    </section>

  )

}



查看完整回答
反对 回复 2021-05-20
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

从其他文件导入它们:

import { usersRowData as filters } from "./otherFile";


查看完整回答
反对 回复 2021-05-20
  • 2 回答
  • 0 关注
  • 166 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信