I am trying to print a nested array (sublist) under object element (value) from the state.list. I tried but did not get the expected result I want. I made two components named Orderlist and Item which hold the nested array and value elements. I could not find where I am doing wrong. Sorry! I am in the learning stage and working on a super small project. Every help would be appreciated.
import React from "react";
import "./styles.css";
const Item = (props) => {
return props.list.map((item)=><li>{item.sublist}</li>);
};
const Orderlist = (props) => {
return props.list.map((element) => (
<ol>
{element.value}
<Item list = {props.list} />
</ol>
));
};
class App extends React.Component {
state = {
list: [
{ value: "Fruit", sublist: ["Banana", "Apple", "Graps"] },
{ value: "Vegetable", sublist: ["Carrat", "Potato", "Mushroom"] },
{ value: "Sports", sublist: ["Cricket", "Badminton", "Football"] },
{ value: "Continent", sublist: ["Asia", "Europe", "Africa"] }
]
};
render() {
return <Orderlist list={this.state.list} />;
}
}
export default App;
outcome got ........
Fruit
BananaAppleGraps
CarratPotatoMushroom
CricketBadmintonFootball
AsiaEuropeAfrica
Vegetable
BananaAppleGraps
CarratPotatoMushroom
CricketBadmintonFootball
AsiaEuropeAfrica
Sports
BananaAppleGraps
CarratPotatoMushroom
CricketBadmintonFootball
AsiaEuropeAfrica
Continent
BananaAppleGraps
CarratPotatoMushroom
CricketBadmintonFootball
AsiaEuropeAfrica