你可以在循环内部创建另一个循环,并使它们同时结束吗?

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

can i make aloop inside a loop and both end at the same time?

问题

{formData.div.map((clsName, index) => (
  <div className={clsName}>
    <input type={formData.type[index]} />
  </div>
))}
英文:

well i have a form data object:

  const formData = {
    type: [
      &quot;text&quot;,
      &quot;text&quot;, 
      &quot;number&quot;, 
      &quot;email&quot;,
      &quot;text&quot;,
      &quot;text&quot;, 
      &quot;text&quot;, 
      &quot;number&quot;],
    div: [
      &quot;col-6&quot;,
      &quot;col-6&quot;,
      &quot;col-6&quot;,
      &quot;col-6&quot;,
      &quot;col-12&quot;,
      &quot;col-4&quot;,
      &quot;col-4&quot;,
      &quot;col-4&quot;,
    ],
  };

i wanted to make 2 loops so the output be like this

&lt;div className=&quot;col-6&quot;&gt;
    &lt;input type=&quot;text&quot;/&gt;
&lt;/div&gt;

&lt;div className=&quot;col-6&quot;&gt;
    &lt;input type=&quot;text&quot;/&gt;
&lt;/div&gt;
.
.
.

I've tried a nasted loop but I already know that the inner loop will end in the first outer loop

{formData.div.map((clsName) =&gt; (
  &lt;div className={clsName}&gt;
     {formData.type.map((type) =&gt; (
       &lt;input type={type} /&gt;
     ))}
  &lt;/div&gt;
))}

答案1

得分: 4

以下是您要翻译的内容:

地图功能的第二个参数是数组中的索引,所以您可以使用该索引来提取相应的 type 条目,就像这样:

{formData.div.map((clsName, index) =&gt; (
  &lt;div className={clsName}&gt;
    &lt;input type={formData.type[index]} /&gt;
  &lt;/div&gt;
))}

但是有一些潜在的问题,比如必须确保 typediv 数组的长度相同。

我建议您改变输入数据的形状,改为使用对象数组,而不是数组对象:

const formData = [
  {
    type: &#39;text&#39;,
    div: &#39;col-6&#39;,
  },
  {
    type: &#39;text&#39;,
    div: &#39;col-6&#39;,
  },
  {
    type: &#39;number&#39;,
    div: &#39;col-6&#39;,
  }
]

这将相关的信息保持在一起,并允许您更直观地进行映射:

{formData.map((entry) =&gt; (
  &lt;div className={entry.div}&gt;
    &lt;input type={entry.type} /&gt;
  &lt;/div&gt;
))}
英文:

The second argument to the map function is the index within the array, so you could use that index to pull the corresponding type entry, like this:

{formData.div.map((clsName, index) =&gt; (
  &lt;div className={clsName}&gt;
    &lt;input type={formData.type[index]} /&gt;
  &lt;/div&gt;
))}

But there are a couple of potential problems, like having to be sure the type and div arrays are the same length.

I'd recommend that you instead change the shape of your input data to be an array of objects, instead of an object of arrays:

const formData = [
  {
    type: &#39;text&#39;,
    div: &#39;col-6&#39;,
  },
  {
    type: &#39;text&#39;,
    div: &#39;col-6&#39;,
  },
  {
    type: &#39;number&#39;,
    div: &#39;col-6&#39;,
  }
]

This keeps the related info together and allows you to map over it much more intuitively:

{formData.map((entry) =&gt; (
  &lt;div className={entry.div}&gt;
    &lt;input type={entry.type} /&gt;
  &lt;/div&gt;
))}

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

发表评论

匿名网友

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

确定