I've been really going at this problem, where I have to group an array of object based on the object's subproperty key/value-pairs. If managed to get it working if it's on the first level with distinct key/values (in this case "name"), but not when it's based on array values in the containing object. Appreciate any help! 

I want to go from:

```
let array = [
    {name: 'Name1', classes: [
        {className: 'class1'}, {className: 'class2'}
    ]}, 
    {name: 'Name2', classes: [
        {className: 'class1'}
    ]}, 
    {name: 'Name3', classes: [
        {className: 'class2'}
    ]}
];

```

into something like:

```
let resultArray = [
    {
      class1: [
        { name: 'Name1', classes: [{ className: 'class1' }, { className: 'class2' }] },
        { name: 'Name2', classes: [{ className: 'class1' }] }
      ]
    },
    {
      class2: [
        { name: 'Name1', classes: [{ className: 'class1' }, { className: 'class2' }] },
        { name: 'Name3', classes: [{ className: 'class2' }] }
      ]
    }
  ];

```

Edit: sorry, didn't add the Minimal, Complete, and Verifiable example. What I had before was:

```
const groupByKey = (arr) =>
  arr.reduce((r, v, i, a, k = v.name) => {
    const rTemp = r;
    rTemp[k] = rTemp[k] ? rTemp[k] : [];
    rTemp[k].push(v);
    return rTemp;
  }, {});

```

which presented it grouped on name (which is not what I wanted obviously, but works on "first level")