2017-07-29 52 views
1

我通过多个对象进行映射。 [{name:"y", country:"US", cities:[obj,obj,ob]},{name:"y", country:"US", cities:[obj,obj,ob]}]在反应中呈现对象的嵌套阵列

我怎样才能嵌套一个循环,所以我第一遍迭代对象,然后遍历(在这个例子中)城市?谢谢!

我的渲​​染功能,无需嵌套的样子看起来是这样的:

render() { 
    const persons = this.state.name.map((item, i) => { 
    return (
     <div> 
     <h5> {item.name} </h5> 
     <h5> {item.country} </h5> 
     //here I would like to show the cities 
     </div> 
    ); 
    }); 
    return (
    <div> 
     <div className = "panel-list"> 
     All: {persons} 
     </div> 
    </div> 
); 
} 

城市对象的例子:

[{visitors:34, rating:4}, 
{visitors:1234, rating:1}, 
{visitors:124, rating:2}] 

回答

3

,您可以利用嵌套地图的绘制在内部子obejcts以及像

 render() { 
      const persons = this.state.name.map((item, i) => { 
       return (
        <div> 
         <h5> {item.name} </h5> 
         <h5> {item.country} </h5> 
         <h4>{item.cities.map((city) => { 
          return (<li>{/* city object details here*/}</li>) 
         })}</h4> 
        </div>); 
      }); 
      return (
      <div> 
       <div className = "panel-list"> 
        All: {persons} 
       </div> 
      </div> 
      ); 
     } 
+0

感谢您的回答。如果我使用你的解决方案,我会得到错误的结果。可以说我(作为例子)5个国家。每个国家有5个城市。然后,我将获得法国加所有5个国家的所有城市,而不仅仅是一个 – javascripting

+0

正如我从您的数据对象“{名称:”y“,国家:”美国“,城市:[obj,obj,ob]}”城市是美国国内的城市,是吗?你的城市对象是怎样的。我想你还需要在你的问题中加上 –

+0

我用国家对象编辑了我的问题。所以如果我会做

{city.visitor}

我不会得到每个国家,但它会看起来像法国,(游客)34,1234,124 – javascripting