r/learnreactjs • • 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

5 comments sorted by

View all comments

Show parent comment

2

u/eb2292 Nov 02 '22

Cool, I got it now. Thank you!