r/learnreactjs • u/eb2292 • Nov 02 '22
Iterating over nested array/dictionary
Hi guys, I am super new to react and JS. I am trying to iterate over a dictionary with nested children which is sent from my Flask backend to react. Why does this code work:
return (
<div>
{Object.keys(data).map((key, index) => (
Object.keys(data[key]).map((y, i) => (
<h2>
{key} : {y} : {data[key][y]}
</h2>
))
))}
</div>
);
But not this code?:
return (
<div>
{Object.keys(data).map((key, index) => (
<div>{key}</div>
Object.keys(data[key]).map((y, i) => (
<h2>
{key} : {y} : {data[key][y]}
</h2>
))
))}
</div>
);
I want to display each outer key once with the children array data underneath it
3
Upvotes
View all comments
Show parent comment
2
u/eb2292 Nov 02 '22
Ah, awesome. I read about rearranging the divs to fix this before I posted but I wasn't able to figure it out. Thanks so much for the example! So in other words, the code stops processing after the first closing div because its the deepest in on the tree?