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

409
Vistas
How to avoid nested forEach if it's possible?

I want to improve my code. It's simple javascript code where I'm fetching some data from gitlab API and I want to get gitlab groups and their members, then I want to for each member display his groups.

let accounts = []


let subGroups = await api.getSubgroups();
subGroups = subGroups.map((group) => {
    const { id, name, full_path } = group;

    return {
        id,
        name,
        full_path
    }
}) 




subGroups = subGroups.map(async (group) => {
  const { id } = group; 
  const groupMembers = await api.getGroupMembers(id);
  return { group_id: id, groupMembers };
});

subGroups = await Promise.all(subGroups);




const map = subGroups.forEach((group) => {

    group.groupMembers.forEach((member) => {
        accounts.push(member)
    })
})

I want first get the list of groups and members of group. Then make array of distinct people and then give them their group. Please how can I remove nested foreach, is it even possible? And also I want to ask how to get only neccessary properties of member (like only name and id) to accounts array

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you have an X by Y data structure, and you need to iterate over all elements, that'll fundamentally require X * Y operations; there's nothing at all wrong with using a nested forEach if that's what the logic requires.

In modern JS, there is a .flatMap method available that'll make things a bit nicer, looking, though:

const accounts = subGroups.flatMap(group => group.groupMembers);

If you also want to extract only certain properties of each groupMember, use the same technique you're doing initially when reorganizing the subGroups array of objects.

const accounts = subGroups
  .flatMap(group => group.groupMembers)
  .map(({ name, id }) => ({ name, id }));

if you want to keep only the name and id properties.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can simply do it by this:

let accounts = [];

let subGroups = await api.getSubgroups();

subGroups.forEach(group => {
  let { id } = group;
  let groupMembers = await api.getGroupMembers(id);
  groupMembers.forEach(accounts.push)
})
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