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

179
Visualizações
Change objects in nested structure using map function

I am trying to change objects in nested map functions. I have one array of objects with nested data.

If I try to do on this way structure of data is changing. I'm getting array of arrays. Only what I need here is to add for each object one property "level". Is it possible to do this with nested map functions?

const res = data.map(itemA => ({ ...itemA,
    level: 'a'
  })
  .subA.map(itemB => ({ ...itemB,
    level: 'b'
  })))

console.log(res)
<script>
  let data = [{
      name: 'testA1',
      subA: [{
          name: 'testB1',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        },
        {
          name: 'testB2',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        }
      ]
    },
    {
      name: 'testA2',
      subA: [{
        name: 'testB1',
        subB: [{
          name: 'testC1',
          subC: [{
            name: 'testD1',
            subD: []
          }]
        }]
      }]
    }
  ]
</script>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use a recursive function?

let data = [{ name: 'testA1', subA: [{ name: 'testB1', subB: [{ name: 'testC1', subC: [] }] }, { name: 'testB2', subB: [{ name: 'testC1', subC: [] }] } ] }, { name: 'testA2', subA: [{ name: 'testB1', subB: [{ name: 'testC1', subC: [{ name: 'testD1', subD: [] }] }] }] } ]

function addprop(arr, level){
    arr.map(x=>{
        const match = Object.keys(x).find(element => {
            if (element.includes('sub')) {
              return true;
            }
          });
          addprop(x[match], level+1)
    })
    return arr.map(x=>(x.level=level,x))
}

console.log(addprop(data, 1))

about 4 years ago · Juan Pablo Isaza Relatório

0

Actually I did it on this way:

const res = data.map(itemA => (
  {...itemA, level: 'a', subA: itemA.subA.map(itemB => (
    {...itemB, level: 'b', subB: itemB.subB.map(itemC => (
      {...itemC, level: 'c'}
    ))}
  ))}
))
console.log(res)
<script>
  let data = [{
      name: 'testA1',
      subA: [{
          name: 'testB1',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        },
        {
          name: 'testB2',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        }
      ]
    },
    {
      name: 'testA2',
      subA: [{
        name: 'testB1',
        subB: [{
          name: 'testC1',
          subC: [{
            name: 'testD1',
            subD: []
          }]
        }]
      }]
    }
  ]
</script>

about 4 years ago · Juan Pablo Isaza 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