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

157
Vistas
How to find specific data in nested object structure in Javascript?

I shared a piece of code which demonstrate I have two types of response. If you take a look at the code below you will find out the response of a service result would be slightly different. In the first type, you have a single data but in the second one, this data has another data in itself.

{
    "data": {
        "hasError": false,
        "developerMessage": null,
        "totalCount": 43
    },
    "hasError": false,
    "developerMessage": null
}

Or:

{
    "data": {
        "hasError": false,
        "developerMessage": null,
        "data": {
            "hasError": false,
            "developerMessage": null,
            "totalCount": 43
        },
        "totalCount": 43
    },
    "hasError": false,
    "developerMessage": null
}

So to handle this predicament I had to first check the result of response and split it into two blocks. The big problem is that for reasons I can't explain here, this hierarchy might expand and I have three data, but what we're sure of is that the data in the response is always the deepest. (The third item in this example)

   return this.httpWrapperService.get(url + tableNameWithSlash, {
            params: params, additionalOptions: {spinnerEnabled: false}
          }).then((result: any) => {

            if (result.data.data) {
              // code
              return [...result.data.data];
            } 

            else if (result.data) {
              // code
              return [...result.data];
            }

          });

I was wondering if there is a better solution to handle this dynamic response.

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

0

You could use recursion to make a solution that works for any depth, e.g. data being on the 4th layer

    .then((result: any) => {
        const findData = (response: any) => {
           return response.data ? findData(response.data):response;
        }
         return [...findData(result.data)];

          });
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