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

193
Visualizações
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 Respostas
Responde à pergunta

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