I have a JSON file that I want to render in react with unknown keys and values (just their types):
{
"Day 1" :{
"Chest": {
"warmUp": ["parallel bar dips"],
"main": ["Bench Press", "Inclined Bench press", "Decline Bench press"],
"secondary": ["Dumbbell Flys", "Cable Crossover Flys", "Pec-deck Fly"]
},
"Biceps" : {
"Lola": ["Barbell Curl", "Preacher Curl"],
"bobo": ["Hammer Curls", "Cable Curl", "Dumbbell Curl"]
}
}
I want to Render them so they look like this
<h2>Day </h2>
<h4>Chest</h4>
<h5>warmUp</h5>
<ul>
<li>Bench Press</li>
<li>Inclined Bench press</li>
<li>Decline Bench press</li>
</ul>
<h5>secondary</h5>
<ul>
<li>Dumbbell Flys</li>
<li>Cable Crossover Flys</li>
<li>Pec-deck Fly</li>
</ul>
I have a solution https://codesandbox.io/s/cold-sun-vprkl?file=/src/App.js
Object.entries(program).map((arrays) => {
return arrays.map((renderArr, render_idx) => {
if (render_idx === 0) {
return render.push(<h2>{renderArr}</h2>);
} else {
for (const [muscleGroup, exerciseGroups] of Object.entries(renderArr)) {
render.push(<h4>{muscleGroup}</h4>);
for (const exerciseCategory in exerciseGroups) {
render.push(<h5>{exerciseCategory}</h5>);
exerciseGroups[exerciseCategory].map(
(exercise, exercise_idx) => {
return render.push(<li>{exercise}</li>);
}
);
;
}
}
}
});
});
but I want to know if there is a better, more elegant way to do this. Also, in my solution I couldn't wrap the ul tags around the list items.