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

191
Vistas
Checking Nesting of objects

There can two sets of objects

First can be a straight forward where is no children

const noChildObj = [{
    "id": 1,
    "label": ["Description"],
    "lines": 1,
    "type": "string",
    "precision": 2,
    "width": 167.8
}, {
    "id": 2,
    "label": ["Information", "Ratio"],
    "lines": 2,
    "type": "number",
    "precision": 2,
    "width": 167.8
}, {
    "id": 3,
    "label": ["Tracking", "Error"],
    "lines": 2,
    "type": "number",
    "precision": 2,
    "width": 167.8
}]

So,in this case, there is no child , so property 'width' is at the topmost layer. Second is where nesting of objects occur

So each object in the array will have a child object


[{
    "id": "257",
    "label": [""],
    "lines": 1,
    "children": [{
        "id": "Description",
        "label": ["Dates"],
        "lines": 1,
        "type": "date",
        "precision": null,
        "width": 839
    }]
}, {
    "id": "258",
    "label": ["Cumulative Return"],
    "lines": 1,
    "children": [{
        "id": 12,
        "label": ["Russell 1000 Value - Price Return"],
        "lines": 1,
        "type": "number",
        "precision": 2,
        "width": 839
    }]
}]

Here each object has a child object where the property width exists

This nesting is not limited to just 1 level It can go upto 4 levels

So my use case requires if the innermost child has width undefined or not

I do realise that through recursion , we can traverse and find out.. But is there any function which can do it in less lines of code..may be in lodash

Please help

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Know only how to do with recursion:

const objArray = [{
    "id": 1,
    "label": ["Description"],
    "lines": 1,
    "type": "string",
    "precision": 2,
    "width": 167.8
}, {
    "id": "257",
    "label": [""],
    "lines": 1,
    "children": [{
        "id": "Description",
        "label": ["Dates"],
        "lines": 1,
        "type": "date",
        "precision": null,
        "width": 839
    }]
}]

const getWidthDeep = (el) => el.children && el.children[0]
  ? getWidthDeep(el.children[0])
  : el.width

const result = objArray.map(getWidthDeep)
console.log(result)

about 4 years ago · Santiago Gelvez 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