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

132
Visualizações
How to filter array of objects with two-level nesting?

I'm have a problem with filtering array of objects with two-levels nesting.

I have 2 arrays, first array is one-level nesting and I have code which work well with it.

const storyId = 2

const arrayOne = [
      {
        name: 'First',
        storyId: 1,
      },
      {
        name: 'Second',
        storyId: 2,
      },
      {
        name: 'Third',
        storyId: 3,
      }
    ]

const result = arrayOne.filter(el => el.storyId === storyId)

But in my case I need to filter array by value of second level of nesting. So I have a next code:

const storyId = 5

let arrayTwo = [
      {
        name: 'First',
        id: 1,
        libraries: {
          nodes: [
            {storyId: 1},
            {storyId: 3},
            {storyId: 5},
          ]
        }
      },
      {
        name: 'Second',
        id: 2,
        libraries: {
          nodes: [
            {storyId: 4},
            {storyId: 5},
            {storyId: 6},
          ]
        }
      },
      {
        name: 'Third',
        id: 3,
        libraries: {
          nodes: [
            {storyId: 3},
            {storyId: 2},
            {storyId: 1},
          ]
        }
      }
    ]

And here I'm expect got array of all first-level objects which have inside libraries->nodes storyId: 5

Tell me please how i can filter it. Preferably in one line

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

0

Not necessarily the most performant way, but this will get only elements with nodes that have a matching storyId

arrayTwo.filter(el => el.libraries.nodes.filter(n => n.storyId === storyId).length > 0)))
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