I'm consuming the following dataset from an external source, which contains a 3 level deep array of objects.
const data = [
{name: "Molly", description: [
{children: [{text: "Likes to run"}] },
{children: [{text: "Likes ice cream"}]},
{children: [{text: "Likes to cars"}] }
]},
{name: "Jay", description: [
{children: [{text: "Likes to race bikes"}]},
{children: [{text: "Likes cake"}]},
{children: [{text: "Likes shoes"}]}
]}
]
I'm wondering if there's an elegant/smarter way to loop over all the array of objects all the way to the text object so I could render its string value.
I'm looking for a vanilla JS solution where I don't end up with several map() functions chained together.
the end result would be to retrieve the text data and render it on a React component such as:
const UserList({ data }) {
return (
<div>
{data.map((user) => (
<SingleUser user={user} />
))}
</div>
);
}
const SingleUser = ({user}) => {
const userDescription = // loop over and transform to render the text data here
return(
<>
<div>{user.name}</div>
<div>{userDescription}</div>
</>
)
}
</div>