React JSX迭代嵌套数组并动态创建元素。

huangapple go评论70阅读模式
英文:

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 = [
	{
		&quot;fields&quot;: [{
			&quot;fNameField-0&quot;: {
				&quot;id&quot;: &quot;fNameField&quot;,
				&quot;value&quot;: &quot;Row1 Col1&quot;
			},
			&quot;lNameField-0&quot;: {
				&quot;id&quot;: &quot;lNameField&quot;,
				&quot;value&quot;: &quot;Row1 Col2&quot;
			}
		}]
	}, {
		&quot;fields&quot;: [{
			&quot;fNameField-1&quot;: {
				&quot;id&quot;: &quot;fNameField&quot;,
				&quot;value&quot;: &quot;Row2 Col1&quot;
			},
			&quot;lNameField-1&quot;: {
				&quot;id&quot;: &quot;lNameField&quot;,
				&quot;value&quot;: &quot;Row2 Col2&quot;
			}
		}]
	}	
]

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

&lt;input type=&quot;hidden&quot; name={fld.id} value={fld.value} /&gt;

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 = [{
  &quot;fields&quot;: [{
    &quot;fNameField-0&quot;: {
      &quot;id&quot;: &quot;fNameField&quot;,
      &quot;value&quot;: &quot;Row1 Col1&quot;
    },
    &quot;lNameField-0&quot;: {
      &quot;id&quot;: &quot;lNameField&quot;,
      &quot;value&quot;: &quot;Row1 Col2&quot;
    }
  }]
}, {
  &quot;fields&quot;: [{
    &quot;fNameField-1&quot;: {
      &quot;id&quot;: &quot;fNameField&quot;,
      &quot;value&quot;: &quot;Row2 Col1&quot;
    },
    &quot;lNameField-1&quot;: {
      &quot;id&quot;: &quot;lNameField&quot;,
      &quot;value&quot;: &quot;Row2 Col2&quot;
    }
  }]
}]

const flattenedObjects = gridItems
  .flatMap(item =&gt; item.fields)
  .flatMap(Object.entries)

flattenedObjects
  .forEach(([key, value]) =&gt;
    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]) =&gt; 
        &lt;input type=&quot;hidden&quot; key={key} name={item.id} value={item.value} /&gt;
    )}
)

答案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 =&gt; o.fields.flatMap(Object.entries))
        .map(([k, fld]) =&gt; &lt;input type=&quot;hidden&quot; name={k} key={k} value={fld.value} /&gt;);

huangapple
  • 本文由 发表于 2023年6月5日 01:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定