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

128
Vistas
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 Respuestas
Responde la pregunta

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