简体   繁体   中英

How to map an array of arrays of arrays with objects in react

I have an array that contains multiple arrays which contain arrays which hold objects. I know right. Sounds confusing. It looks like this

这是主数组

这就是每个数组的内部结构

每个内部数组包含对象

Each Internal Array has two arrays, one in the 0 index and the other in the first index. These in turn also contain objects. Now my problem is how can I map from the top arrays to get to the lowests object to be able to do something like {array.name} in react. 这不起作用。页面上没有任何内容。 This does not work. Nothing gets rendered onto the page

This is how the array looks like. I removed the other two internal arrays to make it look less messy.

 const data = [ [ [ {name: "Michael Norman", house: "9", class: "3B2"} ], [ {name: "Ronald Eyeson", house: "9", class: "3D3"}, {name: "Kingsley Buadi", house: "9", class: "3N"}, {name: "Lommo", house: "9", class: "3H"} ] ] ] console.log(data)

Try to run this code:

 const data = [ [ [ {name: "Michael Norman", house: "9", class: "3B2"} ], [ {name: "Ronald Eyeson", house: "9", class: "3D3"}, {name: "Kingsley Buadi", house: "9", class: "3N"}, {name: "Lommo", house: "9", class: "3H"} ] ], [ [ {name: "Michael Norman1", house: "9", class: "3B2"} ], [ {name: "Ronald Eyeson1", house: "9", class: "3D3"}, {name: "Kingsley Buadi1", house: "9", class: "3N"}, {name: "Lommo1", house: "9", class: "3H"} ] ] ];
 <div> {Object.keys(data).map((key) => { return( <div key={key}> {data[key].map((dataItem) => { return( <div> {Object.keys(dataItem).map((innerDataItem) => { return ( <h1>{dataItem[innerDataItem].name} </h1> ); }) } </div> ) })} </div> ) })} </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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