DEV Community

MOHSIN ALI SOOMRO
MOHSIN ALI SOOMRO

Posted on

3 2

My first job interview question?

How to get loop throw and structure data.
loop throw questions array and get comments with respect of the questionId and replies with respect of commentId

Example

let data = {
    questions: [
        {
            questionId: 1,
            questionText: "Question 1",
        },
        {
            questionId: 2,
            questionText: "Question 2",
        },
        {
            questionId: 3,
            questionText: "Question 3",
        },
        {
            questionId: 4,
            questionText: "Question 4",
        },
    ],
    comments: [
        {
            commentId: 1,
            questionId: 1,
            commentText: "Comment 1",
        },
        {
            commentId: 2,
            questionId: 1,
            commentText: "Comment 2",
        },
        {
            commentId: 3,
            questionId: 1,
            commentText: "Comment 3",
        },
        {
            commentId: 4,
            questionId: 3,
            commentText: "Comment 4",
        },
        {
            commentId: 5,
            questionId: 2,
            commentText: "Comment 5",
        },
    ],
    replies: [
        {
            commentId: 1,
            replyId: 1,
            replyText: "Reply 1",
        },
        {
            commentId: 2,
            replyId: 2,
            replyText: "Reply 2",
        },
        {
            commentId: 3,
            replyId: 3,
            replyText: "Reply 3",
        },
        {
            commentId: 3,
            replyId: 4,
            replyText: "Reply 4",
        },
        {
            commentId: 5,
            replyId: 5,
            replyText: "Reply 5",
        },
    ],
};

Enter fullscreen mode Exit fullscreen mode

Try yourself to prepare interview if stuck blow is the solution
Desire output:

[
  {
    questions: { questionId: 1, questionText: 'Question 1' },
    comments: { commentId: 1, questionId: 1, commentText: 'Comment 1' },
    replies: [ [Object] ]
  },
  {
    questions: { questionId: 1, questionText: 'Question 1' },
    comments: { commentId: 2, questionId: 1, commentText: 'Comment 2' },
    replies: [ [Object] ]
  },
  {
    questions: { questionId: 1, questionText: 'Question 1' },
    comments: { commentId: 3, questionId: 1, commentText: 'Comment 3' },
    replies: [ [Object], [Object] ]
  },
  {
    questions: { questionId: 2, questionText: 'Question 2' },
    comments: { commentId: 5, questionId: 2, commentText: 'Comment 5' },
    replies: [ [Object] ]
  },
  {
    questions: { questionId: 3, questionText: 'Question 3' },
    comments: { commentId: 4, questionId: 3, commentText: 'Comment 4' },
    replies: []
  }
]

Enter fullscreen mode Exit fullscreen mode

Solution 👏 👏 👏

function generateOutput(data) {
    const { questions, comments, replies } = data;
    // CODE HERE
    let output = [];
  questions.map((i)=>{
      comments.map((c)=>{
        const reply = replies.filter((r)=>r.commentId===c.commentId)
        if(c.questionId===i.questionId){
          output.push({questions:i,comments:c,replies:reply})
        }
      })
  })
    return output;
}

let output = generateOutput(data);

console.log(output);

Enter fullscreen mode Exit fullscreen mode

Thanks for reading, if it is helpful comment blow with Thanks 😃

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay