Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda