如何在 React 中渲染嵌套的数组元素?

IT技术 reactjs rendering array.prototype.map
2021-05-15 14:02:43

我想呈现嵌套的数组元素。为了渲染元素,我使用了 .map 但它不适用于第二个数组。

使用 list=[{value: 'One', list:[{value: 'abc', selected: false}, {value: 'efg', selected: false}]}, {value: 'Two', list: [ {value: 'psr', selected: false}]}];

   list.map((item, index) => {
        return (
          <div key={index}>
            <ul >{item.value}</ul>
            item.list.map((subitem, i) => {
              return (
                 <ul >{subitem.value}</ul>
              )
            })
          </div>
        )
      })

我在这里错过了什么吗?

谢谢

1个回答

试试这个。在第二次之前错过了{ }map

 list.map((item, index) => {
            return (
              <div key={index}>
                <ul >{item.value}</ul>
               {
                item.list.map((subitem, i) => {
                  return (
                     <ul ><li>{subitem.value}</li></ul>
                  )
                })
               }
              </div>
            )
          }

演示:https : //jsfiddle.net/jwm6k66c/2611/