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

137
Views
Getting undefined result when trying to loop and filtering array

I am currently working with objects and arrays in nodejs in conjuction with filters. I am currenlty facing difficulty in figuring out how to properly traverse an object and filter for the desired results. Instead I am getting undefined. I have an object users and I am wanting to filter for each user configuration that has active === true and then ultimately display every users configuration with that filter in the final result. What is the right/best way to approach this? Should I use map?

Current Result:

undefined

Desired Result:

[
    {
        email: 'test1@email.com',
        active: true
    },
    {
        email: 'test3@email.com',
        active: true
    },
    {
        email: 'test4@email.com',
        active: true
    }
]

Code:

const users = [
    {
      name: 'User1',
      configuration: [ 
          {
            email: 'test1@email.com',
            active: true
          },
          {
            email: 'test2@email.com',
            active: false
          }
      ],
    },
    {
      name: 'User2',
      configuration: [ 
          {
            email: 'test3@email.com',
            active: true
          },
          {
            email: 'test4@email.com',
            active: true
          }
      ],
    },
];

const result = users.forEach(user => user.configuration.filter( x => {

    let {
        active
    } = x;

    return active === true;
}));

console.log(result);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you can use flatMap for this. forEach always returns undefined. usually if you want to return some array use map but since filter also returns an array you need to flat it to get your desired result hence flatMap

const users = [{name: 'User1',configuration: [ {email: 'test1@email.com',active: true},{email: 'test2@email.com',active: false}],},{name: 'User2',configuration: [ {email: 'test3@email.com',active: true},{email: 'test4@email.com',active: true}],},];

const result = users.flatMap(user => user.configuration.filter( x => x.active===true));

console.log(result);

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!