I have created a JavaScript array that contains some other objects. the subobjects contain arrays. Now I want to iterate through the entire parent object in React. I use the following approach but it does not work:
Array
const users =[
{
id:1,
username: "user1",
path: "/user",
listItems: {lists:["list-1", "List-2", "List-3", "List-4", "List-5"], path: ["/list-1", "/List-2", "/List-3", "/List-4", "/List-5"]}
},
{
id:2,
username: "user2",
path: "/user2",
listItems: {lists:["list-1", "List-2", "List-3", "List-4", "List-5"], path: ["/list-1", "/List-2", "/List-3", "/List-4", "/List-5"]}
},
{
id:3,
username: "user3",
path: "/user3",
listItems: {lists:["list-1", "List-2", "List-3", "List-4", "List-5"], path: ["/list-1", "/List-2", "/List-3", "/List-4", "/List-5"]}
}
]
usersInfo component
export default function usersInfo(props){
const users = props.users;
return(
<div className="container">
{users.map((item)=>{
return(
<section className="user">
<h1>{item.username}</h1>
<p>{item.path}</p>
<Link href={item.path}>Visit</Link>
<p>Users lists</p>
<ul>
{item.listItems.map((topic)=>{
<Link href={topic.path}<li>{topic.lists}</li>
})}
</ul>
</section>
)})}
</div>
)}
Using usersInfo coponent in React:
<usersInfo users = {users}/>
Everything works fine. But I get an issue when trying to map through the listItems object which contains two arrays lists and path.
How do I solve this?
I expect the output like this:
