简体   繁体   English

如何映射嵌套对象

[英]How to map a nested object

I have to populate the li element with data stored as a JSON object.我必须用存储为 JSON 对象的数据填充 li 元素。 With "title" it works simple.有了“标题”,它的工作很简单。 But it's not when talking about name's values.但这不是谈论名称的价值时。 How can I map the subMenu object to get the name?如何映射 subMenu 对象以获取名称?

    <ul>
      {data.map(({ title, subMenu }) => (
                <li className="mobileMenu-body-nav-item">
                    <button className="mobileMenu-body-nav-item-btn"> 
                 *** here I have to put name ***
                    </button>
                </li>
     ))}
    </ul>
         

JSON object JSON 对象

[
   {
    "title": "Breeds",
    "subMenu": [
        {
            "id": 1,
            "name": "Dog Breeds"
        },
        {
            "id": 2,
            "name": "Cat Breeds"
        }
    ]
  },
  {
    "title": "About Pet Adoption",
    "subMenu": [
        {
            "id": 3,
            "name": "About Dog Adoption"
        },
        {
            "id": 4,
            "name": "About Cat Adoption"
        }
      ]
   }
 ]

You can just call map again, like this:您可以再次调用map ,如下所示:

 <ul>
    {data.map(({ title, subMenu }) => (
        <li className="mobileMenu-body-nav-item">
            <button className="mobileMenu-body-nav-item-btn">
                {subMenu.map(({ name }) => (<span>{name}</span>))}
            </button>
        </li>
    ))}
</ul>

Change the <span> tag to match however you want this content to be rendered.更改<span>标签以匹配您希望呈现的内容。

Also, if this is React, don't forget to set the key prop appropriately when using map :另外,如果这是 React,请不要忘记在使用map时适当地设置key属性

<ul>
    {data.map(({ title, subMenu }) => (
        <li key={title} className="mobileMenu-body-nav-item">
            <button className="mobileMenu-body-nav-item-btn">
                {subMenu.map(({ name }) => (<div key={name}>{name}</div>))}
            </button>
        </li>
    ))}
</ul>

As noted in the comments on the accepted answer div elements are not valid children of buttons ( related question ) and while one should assign a key to mapped elements in React using the index of the iterated array is not always ideal.正如对已接受答案的评论中所指出的, div元素不是buttons有效子元素( 相关问题),虽然应该使用迭代数组的索引为 React 中的映射元素分配一个key并不总是理想的。 (see the Docs or related article from TJCrowder's comment). (请参阅 TJCrowder 评论中的文档相关文章)。

Given that you are mapping a nested list it seems more appropriate to structure it as such.鉴于您正在映射嵌套列表,因此将其结构化似乎更合适。 Here using the title as the outer li key (though a more definite unique property would be better) and the subMenu.id as a key for the inner li .这里使用title作为外部li键(尽管更明确的唯一属性会更好)和subMenu.id作为内部li的键。

<ul>
  {data.map(({ title, subMenu }) => (
    <li key={title} className='mobileMenu-body-nav-item'>
      <ul>
        {subMenu.map(({ id, name }) => (
          <li key={id}>
            <button className='mobileMenu-body-nav-item-btn'>{name}</button>
          </li>
        ))}
      </ul>
    </li>
  ))}
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM