Skip to content

Instantly share code, notes, and snippets.

@brittanydionigi
Created July 12, 2017 16:24
Show Gist options
  • Save brittanydionigi/f6b886bdf8644876c578d4113554ccb5 to your computer and use it in GitHub Desktop.
Save brittanydionigi/f6b886bdf8644876c578d4113554ccb5 to your computer and use it in GitHub Desktop.
JS Challenge mod 4

Prompt

Extend the native Array data type to allow a groupBy method so that when given an array of objects, you can group each array item by a specified property.

For example, given an array of Turing students:

let students = [
  { name: "Louisa", module: "4FE", track: "frontEnd" },
  { name: "Nathaniel", module: "3FE", track: "frontEnd" },
  { name: "Robbie", module: "3BE", track: "backEnd" },
  { name: "Brenna", module: "4FE", track: "frontEnd" },
  { name: "Brittany", module: "2BE", track: "backEnd" },
  { name: "Taylor", module: "3BE", track: "backEnd" }
];

Calling:

students.groupBy('track');

Will return:

{
  frontEnd: [
    { name: "Louisa", module: "4FE", track: "frontEnd" },
    { name: "Nathaniel", module: "3FE", track: "frontEnd" },
    { name: "Brenna", module: "4FE", track: "frontEnd" }
  ],
  backEnd: [
    { name: "Taylor", module: "3BE", track: "backEnd" },
    { name: "Brittany", module: "2BE", track: "backEnd" },
    { name: "Robbie", module: "3BE", track: "backEnd" }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment