【问题标题】:Lodash groupBy id of each item of nested array of objectLodash groupBy 对象嵌套数组中每一项的 id
【发布时间】:2022-01-06 10:26:12
【问题描述】:

我有以下输入作为 clubs 的示例,其中包含一个属性 players,它是一个对象数组。

输入

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,
      },
    ],
  },
];

我想groupBy每个俱乐部的每个player.userIds,并且应该为每个球员有一个俱乐部的价值,以获得以下输出。

期望的输出

{
  '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] }],
};

我试过了

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;
}, {});

但它通过clubId 和俱乐部中每个球员的值返回组

{
  '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] },
  ],
};

【问题讨论】:

    标签: javascript arrays lodash


    【解决方案1】:

    替换

    const id = player.clubId;
    

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

    【讨论】:

    • 这个按正确的属性分组,但是每个属性的值是球员而不是俱乐部,所以我想我确实需要push(a)
    • 对不起,请看编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    相关资源
    最近更新 更多