Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!