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

104
Visualizações
How to map() on nested array in a clear way

Newbie here. Please review the following piece of code and suggest how would you write it in a more clear way, because as a newbie, all my ways are way more procedural etc.

  arr.map((p) =>
    p.id === pId
      ? p.bList.map((b) =>
          b.id === bId
            ? b.fList.map((f) =>
                f.id === fId ? console.log('magic happens here') : f
              )
            : b
        )
      : p
  );

I didn't put real names in purpose, so you could see how unreadable it is and very confusing to understand what is going on.

Update: I'm trying to run an operation(get data/modify/delete etc) on a nested array. I'm using Immer, so I actually don't need to take care of mutating the original data, so there's no specific reason to use map() for example.

Update2: Here is an example of the array I have:

    const arr = [
        {
          id: 123,
          // data,
          bList: [
            {
              id: 24,
              // data,
              fList: [
                {
                  id: 51,
                  // data,
                }
              ]
            }
          ]
        },
      ]

So at the above example, I try to modify the fList object that match id 51

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

0

You should chain it with a pipeline. For example like this:

  arr
 .filter(p => p.id === pId)
 .flatMap(p => p.bList)
 .filter(b => b.id === bId)
 .forEach(elm => console.log(elm))
about 4 years ago · Juan Pablo Isaza Relatório

0

If I got it right, you want to modify an object deeply nested in your array. In this case, you can chain a series of Array.prototype.find() using optional chaining (?.) and do something like:

const itemToUpdate = arr.find(p => p.id === pId)?.
    bList.find(b => b.id === bId)?.
    fList.find(f => f.id === fId)

if (itemToUpdate) {
    console.log('magic happens here')
}

Demo:

const arr = [{
    id: 'foo',
    bList: [{
      id: 'bar',
      fList: [{
          id: 'baz',
          key: 'value'
        },
        {
          id: 'baz2',
          key: 'value'
        }
      ]
    }]
  },
  {
    id: 'goo',
    bList: [{
      id: 'car',
      fList: [{
          id: 'caz',
          key: 'value'
        },
        {
          id: 'caz2',
          key: 'value'
        }
      ]
    }]
  }
]

const pId = 'foo';
const bId = 'bar';
const fId = 'baz';

const itemToUpdate = arr.find(p => p.id === pId)?.
  bList.find(b => b.id === bId)?.
  fList.find(f => f.id === fId);

if (itemToUpdate) {
  itemToUpdate.key = 'new value'
}

console.log(arr);

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