Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
JavaScript Recursion identify last element

I have a piece of code here, and i wanted to identify the last element paragraph 3 and add some text like - last item and the output would be paragraph 3 - last item.

I would prefer if its recursive since there is no limit on the number children in an object.

obj = {
    content: [
        { text: "paragraph 1" },
        { 
            content: [
                { text: "paragraph 2" },
            ]
        },
        { text: "paragraph 3" },
    ]
}

Another example would be this, its output should be paragraph 5 - last item

obj = {
    content: [
        { text: "paragraph 1" },
        { 
            content: [
                { text: "paragraph 2" }
            ]
        },
        { text: "paragraph 3" },
        { 
            content: [
                { text: "paragraph 4" },
                { 
                    content: [
                        { text: "paragraph 5" }
                    ]
                }
            ]
        }
    ]
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

A simple implementation. Checks the key name and if it's content, it'll recall itself with the last element. Else, it'll return it.

const obj1 = {
  content: [{
    text: "paragraph 1"
  }, {
    content: [{
      text: "paragraph 2"
    }]
  }, {
    text: "paragraph 3"
  }]
};

const obj2 = {
  content: [{
      text: "paragraph 1"
    },
    {
      content: [{
        text: "paragraph 2"
      }]
    }, {
      text: "paragraph 3"
    },
    {
      content: [{
          text: "paragraph 4"
        },
        {
          content: [{
            text: "paragraph 5"
          }]
        }
      ]
    }
  ]
}

function getLatestParagraph(obj) {
  for (const key in obj) {
    if (key === "content") return getLatestParagraph(obj[key].pop());
    return obj[key];
  }
}

console.log(getLatestParagraph(obj1))
console.log(getLatestParagraph(obj2))

about 4 years ago · Juan Pablo Isaza Relatório

0

One way is to loop and get a reference to text in one variable and update it each loop. After all the loops end the variable will be pointing to tha last text so you just add ' - last item' to it.

Here is a working example:

const obj1 = {
  content: [{
      text: 'paragraph 1'
    },
    {
      content: [{
        text: 'paragraph 2'
      }]
    },
    {
      text: 'paragraph 3'
    },
    {
      content: [{
          text: 'paragraph 4'
        },
        {
          content: [{
            text: 'paragraph 5'
          }]
        },
      ],
    },
  ],
}

const obj2 = {
  content: [{
      text: 'paragraph 1'
    },
    {
      content: [{
        text: 'paragraph 2'
      }]
    },
    {
      text: 'paragraph 3'
    },
  ],
}

let last

function getLast(o) {
  for (let key in o) {
    if (o[key] instanceof Array) {
      for (let value of o[key]) {
        getLast(value)
      }
    } else if (key == 'text') {
      last = o
    }
  }
}

getLast(obj1)
last.text += ' - last item'
getLast(obj2)
last.text += ' - last item'

console.log('\nobj1 = ', JSON.stringify(obj1, null, 1))
console.log('\nobj2 = ', JSON.stringify(obj2, null, 1))

Edit: If what you actually wanted is just get the text of the last item then you can also do this:

const obj1 = {
  content: [{
      text: 'paragraph 1'
    },
    {
      content: [{
        text: 'paragraph 2'
      }]
    },
    {
      text: 'paragraph 3'
    },
    {
      content: [{
          text: 'paragraph 4'
        },
        {
          content: [{
            text: 'paragraph 5'
          }]
        },
      ],
    },
  ],
}

const obj2 = {
  content: [{
      text: 'paragraph 1'
    },
    {
      content: [{
        text: 'paragraph 2'
      }]
    },
    {
      text: 'paragraph 3'
    },
  ],
}


let last
function getLast(o) {
  for (let key in o) {
    if (o[key] instanceof Array) {
      for (let value of o[key]) {
        getLast(value)
      }
    } else if (key == 'text') {
      last = o
    }
  }
  return last.text
}

const last1 = getLast(obj1)
console.log(last1)
const last2 = getLast(obj2)
console.log(last2)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can map on the content property and return each element. If the content property exists, same condition will run again, else it will print the text property.

let lastItem = {};

function recursive(node) {
    if (node.content) {
        node.content.map(element => recursive(element));
    } else if (node.text) {
        lastItem = node.text;
    }
}

obj.content.map(node => recursive(node));
console.log(lastItem + " - last item");

Working demo: https://jsfiddle.net/4f7Lt3ue/

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda