Skip to content
Blog Snippets About

Group By


Group an array of objects by the given key field. This will return a Map where the keys are the values of the given key field, and the values are the objects from the array.

This is best used in Typescript due to the type safety it provides. Without that you may run into issues with:

  1. Objects that don’t have the key specified
  2. Non-object values in the array
  3. Non-array inputs

I have added an extra snippet that includes some basic safety checks for these scenarios, but in most cases you can avoid these issues by using Typescript - As long as you correctly validate data that is…