/ts_group_by_key

TS Basics task. Group data by key.

Primary LanguageTypeScriptGNU General Public License v3.0GPL-3.0

Group by key

Implement a function groupByKey that takes an array of similar objects (books, students, etc.) and a string key that is one of possible item keys (key: keyof T). The function returns an object where all the items are grouped by values stored under a given key (see the examples).

const books = [
  { id: 1, color: 'red', country: 'Ukraine' },
  { id: 1, color: 'red', country: 'Italy' },
  { id: 1, color: 'green', country: 'Ukraine' },
;
// Group books by color
groupByKey(books, 'color') === {
  'red': [
    { id: 1, color: 'red', country: 'Ukraine' },
    { id: 1, color: 'red', country: 'Italy' },
  ],
  'green': [
    { id: 1, color: 'green', country: 'Ukraine' },
  ],
}
// group books by country
groupByKey(books, 'country') === {
  'Ukraine': [
    { id: 1, color: 'red', country: 'Ukraine' },
    { id: 1, color: 'green', country: 'Ukraine' },
  ],
  'Italy': [
    { id: 1, color: 'red', country: 'Italy' },
  ],
}