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

261
Vistas
Lodash groupBy id of each item of nested array of object

I have the following input as an example of clubs that includes a property players which is an array of objects.

Input

const clubs = [
  {
    id: 5,
    name: 'Club Name',
    creatorId: 10,
    players: [
      {
        userId: 2, // group by this property
        name: 'Player name 1',
        clubId: 5,
      },
      {
        userId: 7, // group by this property
        name: 'Player name 2',
        clubId: 5,
      },
    ],
  },
  {
    id: 6,
    name: 'Club Name 2',
    creatorId: 2,
    players: [
      {
        userId: 7, // group by this property
        name: 'Player name 3',
        clubId: 6,
      },
      {
        userId: 8, // group by this property
        name: 'Player name 4',
        clubId: 6,
      },
      {
        userId: 22, // group by this property
        name: 'Player name 5',
        clubId: 6,
      },
    ],
  },
];

I want to groupBy each of the player.userIds of each club and should have a value of the club for each player, to have the following output.

Desired Output

{
  '2': [{ id: 5, name: 'Club Name', creatorId: 10, players: [Array] }],
  '7': [
    { id: 5, name: 'Club Name', creatorId: 10, players: [Array] },
    { id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] },
  ],
  '8': [{ id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] }],
  '22': [{ id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] }],
};

I have tried

const byPlayer = allClubs.reduce((b, a) => {
  a.players.forEach((player) => {
    const id = player.clubId;
    const clubsByPlayer = b[id] || (b[id] = []);
    clubsByPlayer.push(a);
  });
  return b;
}, {});

But it returned the group by the clubId and a value of each player in the club

{
  '5': [
    { id: 5, name: 'Club Name', creatorId: 10, players: [Array] },
    { id: 5, name: 'Club Name', creatorId: 10, players: [Array] },
  ],
  '6': [
    { id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] },
    { id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] },
    { id: 6, name: 'Club Name 2', creatorId: 2, players: [Array] },
  ],
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Replace

const id = player.clubId;

with

const id = player.userId;

const
    clubs = [{ id: 5, name: 'Club Name', creatorId: 10, players: [{ userId: 2, name: 'Player name 1', clubId: 5 }, { userId: 7, name: 'Player name 2', clubId: 5 }] }, { id: 6, name: 'Club Name 2', creatorId: 2, players: [{ userId: 7, name: 'Player name 3', clubId: 6 }, { userId: 8, name: 'Player name 4', clubId: 6 }, { userId: 22, name: 'Player name 5', clubId: 6 }] }],
    byPlayer = clubs.reduce((b, a) => {
        a.players.forEach((player) => {
            (b[player.userId] ??= []).push(a);
        });
        return b;
    }, {});

console.log(byPlayer)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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