[英]How to loop array and output the values inside an array of objects as 'value' and 'label'?
I have the following data as array of objects in a separate file 我在单独的文件中将以下数据作为对象数组
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"
}
}
}
]
inside my react component I have these filters data for dropdown hardcoded as value and label: 在我的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" }]
}
];
How can I loop an array output every name as value and label inside my react component so I can display those in a list. 我如何循环数组将每个名称输出为我的react组件内的值和标签,以便可以在列表中显示它们。
从其他文件导入它们:
import { usersRowData as filters } from "./otherFile";
Are you saying displaying like option and value? 您是说要显示选项和价值吗?
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>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.