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

90
Vistas
Return last value with recursion - Javascript

Hi all I have following data:

const section = {
  fileds: [
    { id: "some Id-1", type: "user-1" },
     {
      child: [
        { id: "some Id-2", type: "user-2" },
        { fileds: [{ id: "kxf5", status: "pending" }] },
        { fileds: [{ id: "ed5t", status: "done" }] }
      ]
    },
    {
      child: [
        { id: "some Id-3", type: "teacher" },
        { fileds: [{ id: "ccfr", status: null }] },
        { fileds: [{ id: "kdpt8", status: "inProgress" }] }
      ]
    }
  ]
};

and following code:


const getLastIds = (arr) =>
  arr.flatMap((obj) => {
    const arrayArrs = Object.values(obj).filter((v) => Array.isArray(v));
    const arrayVals = Object.entries(obj)
      .filter(([k, v]) => typeof v === "string" && k === "id")
      .map(([k, v]) => v);
    return [...arrayVals, ...arrayArrs.flatMap((arr) => getLastIds(arr))];
  });

console.log(getLastIds(section.fileds));

// output is (7) ["some Id-1", "some Id-2", "kxf5", "ed5t", "some Id-3", "ccfr", "kdpt8"]

My code doing following, it printing in new array all ids.

It's working but I don't need all ids.

I need to return only last id in array and I should use recursion. The output should be

(4) [" "kxf5", "ed5t", "ccfr", "kdpt8"]

P.S. here is my code in codesandbox

Is there a way to solve this problem with recursion? Please help to fix this.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can do it with reduce.

function getLastIds (value) {
    return value.reduce((prev, cur) => {
        if (cur.id) {
            return [ ...prev, cur.id ];
        } else {
            let key = ('child' in cur) ? 'child' : 'fileds';
            return [ ...prev, ...getLastIds (cur[key]) ]
        }
    }, []);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could check if a certain key exists and take this property for mapping id if status exists.

const
    getValues = data => {
        const array = Object.values(data).find(Array.isArray);
        return array
            ? array.flatMap(getValues)
            : 'status' in data ? data.id : [];
    },
    section = { fileds: [{ id: "some Id-1", type: "user-1" }, { child: [{ id: "some Id-2", type: "user-2" }, { fileds: [{ id: "kxf5", status: "pending" }] }, { fileds: [{ id: "ed5t", status: "done" }] }] }, { child: [{ id: "some Id-3", type: "teacher" }, { fileds: [{ id: "ccfr", status: null }] }, { fileds: [{ id: "kdpt8", status: "inProgress" }] }] }] },
    result = getValues(section);

console.log(result);

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