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

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

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