I'm just getting started with React and am trying to loop through an object that is nested inside two arr's. Here's what it looks like:
[
{
"views": "1,001,023",
"likes": "110,985",
"channel": "Red Cow",
"image": "https://i.imgur.com/l2Xfgpl.jpg",
"comments": [
{
"name": "Micheal Lyons",
"comment": "They BLEW the ROOF off at their last event, once everyone started figuring out they were going. This is still simply the greatest opening of an event I have EVER witnessed.",
"likes": 0,
},
{
"name": "Gary Wong",
"comment": "Every time I see him shred I feel so motivated to get off my couch and hop on my board. He’s so talented! I wish I can ride like him one day so I can really enjoy myself!",
"likes": 0,
},
{
"name": "Theodore Duncan",
"comment": "How can someone be so good!!! You can tell he lives for this and loves to do it every day. Every time I see him I feel instantly happy! He’s definitely my favorite ever!",
"likes": 0,
}
]
},
I've been trying to use .map to loop through it to pull the data of "name, comment and likes", so that it will be displayed on the website. Meaning, I have to go down two levels. First I loop through the initial arr > then access the comments arr > then loop through each index of the object. But with a loop I can only go down one level instead of two. Here's what I had attempted as well... looping through the first arr and then reassigning that value to another loop but I keep getting the message that firstLevel.map is undefined inside the return.
import VideoDetails from "../../data/video-details.json";
import Comment from "../Comment/Comment";
function CommentList() {
const firstLevel = VideoDetails.forEach((i) => {
return i.comments;
});
return (
<>
{firstLevel.map((event) => {
return (
<Comment
image={event.comments}
name={event.name}
timestamp={event.timestamp}
/>
);
})}
</>
);
}
export default CommentList;
The loop should output the following:
name: Michael Lyons, comment: They Below the ROOF off at their last event..., like: 0
name: Gary Wong, comment: Every time I see him..., likes: 0
and so on for each of the objects.