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

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

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 Denunciar

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 Denunciar

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 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