Skip to content

Instantly share code, notes, and snippets.

@robbiejaeger
Created May 3, 2017 14:03
Show Gist options
  • Select an option

  • Save robbiejaeger/2b7d678242dd336589b2cd9072ae4a21 to your computer and use it in GitHub Desktop.

Select an option

Save robbiejaeger/2b7d678242dd336589b2cd9072ae4a21 to your computer and use it in GitHub Desktop.

JS Native Data Types Challenge

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