简体   繁体   English

如何使用和不使用Lodash通过多个键对对象数组进行分组?

[英]How to group array of objects by multiple keys with and without Lodash?

export default [
  {
    id: 101,
    league: {
      id: "500",
      name: "EU Champions League"
    },
    group: {
      id: "990",
      name: "Group A"
    }
  },
  {
    id: 102,
    league: {
      id: 600,
      name: "Super Lig"
    }
  },
  {
    id: 103,
    league: {
      id: 500,
      name: "EU Champions League"
    }
  },
  {
    id: 104,
    league: {
      id: 500,
      name: "EU Champions League"
    }
  },
  {
    id: 105,
    league: {
      id: 500,
      name: "EU Champions League"
    },
    group: {
      id: "991",
      name: "Group B"
    }
  },
  {
    id: 106,
    league: {
      id: 500,
      name: "EU Champions League"
    },
    group: {
      id: "991",
      name: "Group B"
    }
  }
];

Here is a live demo, but it's nowhere near what I'm expecting it, it's good for reference to determine what I'm trying to do and obviously failed!这是一个现场演示,但与我期望的相差甚远,可以参考以确定我正在尝试做什么并且显然失败了!

https://codesandbox.io/s/js-group-array-of-object-by-multiple-keys-8b4s0 https://codesandbox.io/s/js-group-array-of-object-by-multiple-keys-8b4s0

The array of objects looks like this;对象数组如下所示;

  • Eu Champions League - Group A欧洲冠军联赛 - A组
  • Super Lig超级联赛
  • EU Champions League欧洲冠军联赛
  • EU Champions League欧洲冠军联赛
  • EU Champions League - Group B欧洲冠军联赛 - B 组
  • EU Champions League - Group B欧洲冠军联赛 - B 组

What I'm trying to do is grouping ones with same league id and ones with same league id + group name, outputting it as an object where object key is the 'league id + group id' (where needed);我想要做的是将具有相同联赛 ID 的和具有相同联赛 ID + 组名的组分组,将其作为对象输出,其中对象键是“联赛 ID + 组 ID”(需要时); So it becomes like so;所以它变成了这样;

{
      // All objects with league id 500 and group A
      '500-990': [{
        id: 101,
        league: {
          id: "500",
          name: "EU Champions League"
        },
        group: {
          id: "990",
          name: "Group A"
        }
      }],
      // All objects with league id 600
      '600': [{
        id: 102,
        league: {
          id: 600,
          name: "Super Lig"
        }
      }],
      // All objects with league id 500 only with NO groups
      '500': [{
        id: 103,
        league: {
          id: 500,
          name: "EU Champions League"
        }
      },
      {
        id: 104,
        league: {
          id: 500,
          name: "EU Champions League"
        }
      }],
      // All objects with league id 500 and group B
      '500-991': [{
        id: 105,
        league: {
          id: 500,
          name: "EU Champions League"
        },
        group: {
          id: "991",
          name: "Group B"
        }
      },
      {
        id: 106,
        league: {
          id: 500,
          name: "EU Champions League"
        },
        group: {
          id: "991",
          name: "Group B"
        }
      }],
}

Basically creating the key based on if group exists, if it does then add on the group letter to the end and create the array.基本上根据组是否存在创建密钥,如果存在,则将组字母添加到末尾并创建数组。

 const groupById = (arr) => { let resp = {}; for (const val of arr) { const key = val.group ? val.league.id + '-' + val.group.name.slice(-1).toLowerCase() : val.league.id; if (!resp[key]) { resp[key] = [val]; } resp[key].push(val); } return resp; } const input = [{ id: 101, league: { id: "500", name: "EU Champions League" }, group: { id: "990", name: "Group A" } }, { id: 102, league: { id: 600, name: "Super Lig" } }, { id: 103, league: { id: 500, name: "EU Champions League" } }, { id: 104, league: { id: 500, name: "EU Champions League" } }, { id: 105, league: { id: 500, name: "EU Champions League" }, group: { id: "991", name: "Group B" } }, { id: 106, league: { id: 500, name: "EU Champions League" }, group: { id: "992", name: "Group B" } }]; console.log(groupById(input));

You could take a combined key, if necessary and group by this value.如有必要,您可以使用组合键并按此值分组。

 const data = [{ id: 101, league: { id: "500", name: "EU Champions League" }, group: { id: "990", name: "Group A" } }, { id: 102, league: { id: 600, name: "Super Lig" } }, { id: 103, league: { id: 500, name: "EU Champions League" } }, { id: 104, league: { id: 500, name: "EU Champions League" } }, { id: 105, league: { id: 500, name: "EU Champions League" }, group: { id: "991", name: "Group B" } }, { id: 106, league: { id: 500, name: "EU Champions League" }, group: { id: "992", name: "Group B" } }], result = data.reduce((r, o) => { const key = o.group ? [o.id, o.group.name].join('-') : o.id; (r[key] ??= []).push(o); return r; }, {}); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Lodash通过多个属性对对象数组进行分组 - Group array of objects by multiple properties with Lodash Lodash如何按值数组对对象数组进行分组? - Lodash how to group array of objects by array of values? JavaScript 使用 Lodash 通过一个或两个键对对象数组进行分组和变换 - JavaScript group and transform array of objects by one or two keys using Lodash lodash将对象数组转换为键的单个数组和值的多个数组 - lodash convert array of objects to single array of keys and multiple array of values 使用 lodash 对对象数组进行分组 - Group array of objects using lodash 如何基于Javascript中的多个键对对象数组进行分组? - How to group an array of objects based on multiple keys in Javascript? lodash通过多个键将数组分组 - lodash grouping an array by multiple keys Javascript ES5:如何在不使用 Lodash 或 Underscore 的情况下按键分组并从键值对象的平面数组创建子对象 - Javascript ES5: How to Group By a key and create Sub objects from a flat array of key-value objects without using Lodash or Underscore 如何使用Lodash在特定字段上的JavaScript对象数组进行分组? - How to group an array of objects in JavaScript on specific fields using Lodash? 使用lodash通过多个键对对象进行分组 - Group an object by multiple keys using lodash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM