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

144
Visualizações
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 Respostas
Responde à pergunta

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