0
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

The array of objects looks like this;

  • Eu Champions League - Group A
  • Super Lig
  • EU Champions League
  • EU Champions League
  • EU Champions League - Group B
  • EU Champions League - Group 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); 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"
        }
      }],
}
6
  • Your example shows that you want to use the key '500' 3 times. Javascript objects cannot have duplicate keys. Commented Nov 9, 2020 at 18:34
  • 1
    please change the wanted result, because you can not have same keys in an object. Commented Nov 9, 2020 at 18:34
  • @NinaScholz, of course :) just updated the expected result, thank you Commented Nov 9, 2020 at 18:38
  • @abney317 Sorry my bad, updated the expected result, thank you Commented Nov 9, 2020 at 18:39
  • where do you get 'a' from the group? Commented Nov 9, 2020 at 18:42

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.