简体   繁体   English

在ReactJS中映射和渲染子数组

[英]Map and Render a Subarray in ReactJS

I have a data object that is constructed like this: 我有一个像这样构造的数据对象:

[{"groupid":"15","items":[
{"id":"42","something":"blah blah blah"},
{"id":"38","something":"blah blah blah"}]},
{"groupid":"7","items":
[{"id":"86","something":"blah blah blah"},
{"id":"49","something":"blah blah blah"}]}]

I am trying to iterate through the groups, then iterate through the items, inside the ReactJS render() method. 我试图遍历各组,然后遍历ReactJS render()方法内的项目。

Here is what I am trying to do: 这是我正在尝试做的事情:

render () {
   return(
      { this.state.dataArray.map( function(group, i) {

          return(<Row><Col>{group.groupid}</Col></Row>

           // { group.items.map( function(activity, j) {
           //     return (<Row><Col>{item.id}</Col></Row>)
           //     }, this) }

         )

       }, this) }
    )
}

The first .map works, and if I remove the return() that is inside the first .map I can run the second .map ... but if I use this structure and uncomment the commented code, I get an error in my Terminal saying there is an Unexpected token at group.item.map 第一个.map有效,如果删除第一个.map内的return() ,则可以运行第二个.map ...,但是如果我使用此结构并取消注释注释的代码,则在终端中会收到错误消息说Unexpected token at group.item.map有一个Unexpected token at group.item.map

Can you provide assistance in how to accomplish this? 您可以提供协助以实现此目标吗?

You can have only single parent element and rest wrapped in it in the return of react elememt. 您只能有一个父元素,其余的都包裹在react elememt的返回中。

Eg: 例如:

return(
<p>Hello</p>
<p>World</p>
)

gives you error 给你错误

It must be wrapped like 它必须像

return( 
    <div> 
      <p>Hello</p> 
      <p>World</p> 
    </div> 
)

wrap your code in a single parent like 'div' 将您的代码包装在“ div”这样的单亲中

` `

      return(
           <div>
              <Row><Col>{group.groupid}</Col></Row>

              { group.items.map( function(activity, j) {
                return (<Row><Col>{item.id}</Col></Row>)
               }, this) }
        </div>
     )

   }, this) }
)

}` }`

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

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