如何遍历该对象以使用下面显示的对象形式创建JSX?

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

How should I traverse the object to create jsx using the form of the object as shown below?

问题

以下是翻译好的部分:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
{
  products: {
    "women": [
      {
        "id": 1,
        "name": "miho",
        "image": "../../thumbs/miho.jpg"
      },
      {
        "id": 2,
        "name": "gaga",
        "image": "../../thumbs/gaga.jpg",
      }
    ],
    "men": [
        {
          "id": 1,
          "name": "teatea",
          "image": "../../thumbs//teatea.jpg"
        }
    ]
  }
}

<!-- language: lang-html -->
<div>
  <div className="keyOfObject">women<div>
  <div className="products-list">
      <div className="product">
        <div className="image" style={{background: url(../../thumbs/miho.jpg)}}/>
        <div className="name">miho<div>
      </div>
      <div className="product">
        <div className="image" style={{background: url(../../thumbs/gaga.jpg)}}/>
        <div className="name">gaga<div>
    </div>
  </div>
<div className="keyOfObject">men<div>
  <div className="products-list">
      <div className="product">
        <div className="image" style={{background: url(../../thumbs/teatea.jpg)}}/>
        <div className="name">teatea<div>
      </div>
  </div>
</div>

<!-- end snippet -->

请注意,上述文本中已经去除了代码部分,只保留了HTML和相关内容。

英文:

I want to create the HTML syntax below.
How do I make an element corresponding to the title with the key of the object, and make a list of details while traversing the array to value?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

{
  products: {
    &quot;women&quot;: [
      {
        &quot;id&quot;: 1,
        &quot;name&quot;: &quot;miho&quot;,
        &quot;image&quot;: &quot;../../thumbs/miho.jpg&quot;
      },
      {
        &quot;id&quot;: 2,
        &quot;name&quot;: &quot;gaga&quot;,
        &quot;image&quot;: &quot;../../thumbs/gaga.jpg&quot;,
      }
    ],
    &quot;men&quot;: [
        {
          &quot;id&quot;: 1,
          &quot;name&quot;: &quot;teatea&quot;,
          &quot;image&quot;: &quot;../../thumbs//teatea.jpg&quot;
        }
    ]
  }
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;div className=&quot;keyOfObject&quot;&gt;women&lt;div&gt;
  &lt;div className=&quot;products-list&quot;&gt;
      &lt;div className=&quot;product&quot;&gt;
        &lt;div className=&quot;image&quot; style={{background: url(../../thumbs/miho.jpg)}}/&gt;
        &lt;div className=&quot;name&quot;&gt;miho&lt;div&gt;
      &lt;/div&gt;
      &lt;div className=&quot;product&quot;&gt;
        &lt;div className=&quot;image&quot; style={{background: url(../../thumbs/gaga.jpg)}}/&gt;
        &lt;div className=&quot;name&quot;&gt;gaga&lt;div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;div className=&quot;keyOfObject&quot;&gt;men&lt;div&gt;
  &lt;div className=&quot;products-list&quot;&gt;
      &lt;div className=&quot;product&quot;&gt;
        &lt;div className=&quot;image&quot; style={{background: url(../../thumbs/teatea.jpg)}}/&gt;
        &lt;div className=&quot;name&quot;&gt;teatea&lt;div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

以下是您要的代码部分的翻译:

export function Component(data) {
  return (
    <div>
      {Object.entries(data).map(([key, productsList]) => {
        return (
          <div key={`${key}-keyOfObject`} className="keyOfObject">
            {key}
          </div>
          <div key={`${key}-productsList`} className="products-list">
            {productsList.map((product) => {
              return (
                <div className="product" key={product.id}>
                  <div className="image" style={`background: url(${product.image})`}></div>
                  <div className="name">{product.name}</div>
                </div>
              );
            })}
          </div>
        );
      })}
    </div>
  );
}

但如果您希望keyOfObject和productsList divs有一个父div,可以这样修改:

export function Component(data) {
  return (
    <div>
      {Object.entries(data).map(([key, productsList]) => {
        return (
          <div key={`${key}-container`} className="container">
            <div key={`${key}-keyOfObject`} className="keyOfObject">
              {key}
            </div>
            <div key={`${key}-productsList`} className="products-list">
              {productsList.map((product) => {
                return (
                  <div className="product" key={product.id}>
                    <div className="image" style={`background: url(${product.image})`}></div>
                    <div className="name">{product.name}</div>
                  </div>
                );
              })}
            </div>
          </div>
        );
      })}
    </div>
  );
}

这将在每个keyOfObject和productsList div的外部包装一个父div,使结构更清晰。

英文:

Something like this:

export function Component(data) {
  return (
    &lt;div&gt;
      {Object.entries(data).map(([key, productsList]) =&gt; {
        return (
          &lt;&gt;
            &lt;div key={`${key}-keyOfObject`} className=&quot;keyOfObject&quot;&gt;
              {key}
            &lt;/div&gt;
            &lt;div key={`${key}-productsList`} className=&quot;products-list&quot;&gt;
              {productsList.map((product) =&gt; {
                return (
                  &lt;div className=&quot;product&quot; key={product.id}&gt;
                    &lt;div className=&quot;image&quot; style={`background: url(${product.image})`}&gt;&lt;/div&gt;
                    &lt;div className=&quot;name&quot;&gt;{product.name}&lt;/div&gt;
                  &lt;/div&gt;
                );
              })}
            &lt;/div&gt;
          &lt;/&gt;
        );
      })}
    &lt;/div&gt;
  );
}

But it will be better if you will have a parent div for keyOfObject and productsList divs

huangapple
  • 本文由 发表于 2023年3月3日 20:38:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627183.html
匿名

发表评论

匿名网友

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

确定