英文:
React JSX Iterate over nested array and dynamically create elements
问题
在我的React应用中,我有一个如下定义的数组:
gridItems = [
{
"fields": [{
"fNameField-0": {
"id": "fNameField",
"value": "Row1 Col1"
},
"lNameField-0": {
"id": "lNameField",
"value": "Row1 Col2"
}
}]
}, {
"fields": [{
"fNameField-1": {
"id": "fNameField",
"value": "Row2 Col1"
},
"lNameField-1": {
"id": "lNameField",
"value": "Row2 Col2"
}
}]
}
]
我想要遍历这个数组,并为每个嵌套对象动态创建下面的隐藏 "input" 元素:
<input type="hidden" name={fld.id} value={fld.value} />
在这种情况下,应该有4个输入元素,对应于每个嵌套对象。
英文:
In my React app, I have an array defined as below;
gridItems = [
{
"fields": [{
"fNameField-0": {
"id": "fNameField",
"value": "Row1 Col1"
},
"lNameField-0": {
"id": "lNameField",
"value": "Row1 Col2"
}
}]
}, {
"fields": [{
"fNameField-1": {
"id": "fNameField",
"value": "Row2 Col1"
},
"lNameField-1": {
"id": "lNameField",
"value": "Row2 Col2"
}
}]
}
]
I want to iterate over this array and create hidden "input" elements for each nested object. So basically, I want to dynamically create the below element
<input type="hidden" name={fld.id} value={fld.value} />
and in this case, there should be 4 input elements corresponding to each of the nested object.
答案1
得分: 1
以下是翻译好的部分:
你可以使用一组简单的数组映射和展开操作来展平树:
const gridItems = [{
"fields": [{
"fNameField-0": {
"id": "fNameField",
"value": "Row1 Col1"
},
"lNameField-0": {
"id": "lNameField",
"value": "Row1 Col2"
}
}]
}, {
"fields": [{
"fNameField-1": {
"id": "fNameField",
"value": "Row2 Col1"
},
"lNameField-1": {
"id": "lNameField",
"value": "Row2 Col2"
}
}]
}]
const flattenedObjects = gridItems
.flatMap(item => item.fields)
.flatMap(Object.entries)
flattenedObjects
.forEach(([key, value]) =>
console.log(`key: ${key}, value.id: ${value.id}, value.value: ${value.value}`)
)
要呈现为JSX,我们只需对结果值进行映射。
您可以在简单的映射调用中使用输出来实现这一点。
return (
{flattenedObjects.map(([key, item]) =>
<input type="hidden" key={key} name={item.id} value={item.value} />
)}
)
请注意,代码中的HTML标记和JSX可能需要适应您的项目环境。
英文:
You can use a simple set of array map & flat operations to flatten out the tree:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const gridItems = [{
"fields": [{
"fNameField-0": {
"id": "fNameField",
"value": "Row1 Col1"
},
"lNameField-0": {
"id": "lNameField",
"value": "Row1 Col2"
}
}]
}, {
"fields": [{
"fNameField-1": {
"id": "fNameField",
"value": "Row2 Col1"
},
"lNameField-1": {
"id": "lNameField",
"value": "Row2 Col2"
}
}]
}]
const flattenedObjects = gridItems
.flatMap(item => item.fields)
.flatMap(Object.entries)
flattenedObjects
.forEach(([key, value]) =>
console.log(`key: ${key}, value.id: ${value.id}, value.value: ${value.value}`)
)
<!-- end snippet -->
To render out to JSX, we can simply map the resulting values.
You can use the output in a simple map call to achieve that.
return (
{flattenedObjects.map(([key, item]) =>
<input type="hidden" key={key} name={item.id} value={item.value} />
)}
)
答案2
得分: 1
你可以首先使用 Array#flatMap
来获取一个扁平数组中的所有字段,然后使用 map
遍历它来创建元素。
return gridItems.flatMap(o => o.fields.flatMap(Object.entries))
.map(([k, fld]) => <input type="hidden" name={k} key={k} value={fld.value} />);
英文:
You can first use Array#flatMap
to get all the fields in a flat array, then map
over that to create the elements.
return gridItems.flatMap(o => o.fields.flatMap(Object.entries))
.map(([k, fld]) => <input type="hidden" name={k} key={k} value={fld.value} />);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论