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

248
Views
Javascript - how can I make this recursive?

I have a function to render comments, in which each comment is stored as an object in an array. Comments can have reply comments, in which they have the exact same html and data to render, just their styling is different (via a CSS modifier class).

How can I make this function recursive? The renderReplies(comment.replies) calls a function that is the exact same as renderComments function, just without the mentioned function call renderReplies (as a reply to a reply is the exact same also in styling terms).

const renderComments = (comments) => {
    commentsElement.innerHTML = '';
    comments.forEach(comment => {
        commentsElement.innerHTML += html;

        // data-id attribute
        const liElements = commentsElement.querySelectorAll('.comment');
        const liElement = liElements.item(liElements.length - 1);
        liElement.setAttribute('data-id', comment.id);

        // author
        liElement.querySelector('.comment__author').innerHTML = comment.user.username;

        // avatar src & alt attributes
        const avatar = liElement.querySelector('.comment__avatar');
        avatar.setAttribute('src', comment.user.image.png);
        avatar.setAttribute('alt', comment.user.username);

        // time since posted

        // content
        const p = liElement.querySelector('.comment__text');
        p.appendChild(document.createTextNode(comment.content));

        // score
        liElement.querySelector('.comment__score b').innerHTML = comment.score;

        // replies
        renderReplies(comment.replies);
    });
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Check whether there's a replies property. If it exists, call the function recursively. Since replies won't have replies of their own, you'll stop there.

const renderComments = (comments) => {
    commentsElement.innerHTML = '';
    comments.forEach(comment => {
        commentsElement.innerHTML += html;

        // data-id attribute
        const liElements = commentsElement.querySelectorAll('.comment');
        const liElement = liElements.item(liElements.length - 1);
        liElement.setAttribute('data-id', comment.id);

        // author
        liElement.querySelector('.comment__author').innerHTML = comment.user.username;

        // avatar src & alt attributes
        const avatar = liElement.querySelector('.comment__avatar');
        avatar.setAttribute('src', comment.user.image.png);
        avatar.setAttribute('alt', comment.user.username);

        // time since posted

        // content
        const p = liElement.querySelector('.comment__text');
        p.appendChild(document.createTextNode(comment.content));

        // score
        liElement.querySelector('.comment__score b').innerHTML = comment.score;

        // replies
        if (comment.hasOwnProperty("replies")) {
            renderComments(comment.replies);
        }
    });
};
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!