Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
how to map an array of objects that has values of array of objects

I have a list of comments that I fetch from my API looking like this. A comment has likes and dislikes alongside the user who posted the like or dislike.

const comments = [
    {
        id: 1,
        comment: "nice",
        author: "jenny",
        likes: [
            {
                id: 1,
                user: "alice"
            },
            {
                id: 2,
                user: "bob"
            }
        ],
        dislikes: [
            {
                
                id: 3,
                user: "sam"
            }
        ]
    },
    {
        id: 2,
        comment: "good job",
        author: "alice",
        likes: [
            {
                id: 2,
                user: "bob"
            }
        ],
        dislikes: [
            {
                
                id: 3,
                user: "sam"
            }
        ]
    }
]

Lets say Alice is logged in.

Now the map to display the comments works fine, but I want to do "nested map" which loops through the likes and dislikes array of objects within the same object array and see if the user of a like or dislike matches who is logged in to display a confirmation.

My current code:

const signedInUser = "alice";

return(
    Object.keys(comments).map((x, index) => {
      const com = comments[x];
  
      <div className="comment" key={index}>
        <p>{com.author}</p>
        <p>{com.comment}</p>
  
        {com.likes.map((x) => {
          {
            x.user === signedInUser ? (
              <p>You liked this</p>
            ) : (
              <button>Like</button>
            );
          }
        })}
      </div>;
    })
  );

I am doing this with react, since I am trying to

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are suppose to use some to get a boolean response if there exist a like by the same user. map return response for every object in the like array.

return(
    Object.keys(comments).map((x, index) => {
      const com = comments[x];
  
      <div className="comment" key={index}>
        <p>{com.author}</p>
        <p>{com.comment}</p>
  
        {com.likes.some((x) => x.user === signedInUser)?
             (
              <p>You liked this</p>
            ) : (
              <button>Like</button>
            );
        }
      </div>;
    })
  );
about 4 years ago · Juan Pablo Isaza Report

0

const renderLikeOrDislikeButton = (arr, text1, ) =>{
   if(arr.some((x) => x.user === signedInUser)){
      return <p>You {text1} this</p>
   }
   return <button>{text2}</button>
}

return(
{
  Object.keys(comments).map((x, index) => {
    const com = comments[x];

    return(
      <div className="comment" key={index}>
        <p>{com.author}</p>
        <p>{com.comment}</p>
        {renderLikeOrDislikeButton(com.likes, "liked", "Like")}
        {renderLikeOrDislikeButton(com.likes, "disliked", "Dislike)}
      </div>
    )
  });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!