JavaScript Array Group

By  on  

Managing, sorting, and manipulating data with JavaScript is a skill we've often delegated to third party libraries like lodash. As the JavaScript language progresses, however, these features eventually get. added to the JavaScript specification. Two such APIs for grouping of Array data are `Array.prototype.group and Array.prototype.groupToMap.

Array.prototype.group

To group an array of objects by a given property, call the group method with function that returns the grouping string:

const teams = [
  { name: "Arsenal", origin: "London", tier: "legendary" },
  { name: "Manchester United", origin: "Manchester", tier: "legendary" },
  { name: "Liverpool", origin: "Liverpool", tier: "legendary" },
  { name: "Newcastle United", origin: "Newcastle", tier: "mid" },
  // Lol, awful club
  { name: "Tottenham", origin: "London", tier: "lol" },
];

const tieredTeams = teams.group(({ tier }) => tier);

The result of the array's group is an object with keys that match the grouping key:

{
  legendary: [
    {name: "Arsenal", origin: "London", tier: "legendary"},
    {name: "Manchester United", origin: "Manchester", tier: "legendary"},
    {name: "Liverpool", origin: "Liverpool", tier: "legendary"}
  ], 
  mid: [
    {name: "Newcastle United", origin: "Newcastle", tier: "mid"}
  ], 
  lol: [
    {name: "Tottenham", origin: "London", tier: "lol"}
  ]
}

Array.prototype.groupToMap

groupToMap returns a Map instance instead of an object literal:

const tieredTeamsMap = teams.group(({ tier }) => tier);

tieredTeamsMap.has('lol') // true

tieredTeamsMap.get('lol') // [{name: "Tottenham", origin: "London", tier: "lol"}]

As of the time of publish, group and groupToMap are only available in Safari. These two methods are crucial to data management moving forward. Whether you're manipulating data on client or server side, these newly added native methods are much welcomed.

Recent Features

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

  • By
    Write Simple, Elegant and Maintainable Media Queries with Sass

    I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...

Incredible Demos

  • By
    MooTools ASCII Art

    I didn't realize that I truly was a nerd until I could admit to myself that ASCII art was better than the pieces Picasso, Monet, or Van Gogh could create.  ASCII art is unmatched in its beauty, simplicity, and ... OK, well, I'm being ridiculous;  ASCII...

  • By
    Image Manipulation with PHP and the GD Library

    Yeah, I'm a Photoshop wizard. I rock the selection tool. I crop like a farmer. I dominate the bucket tool. Hell, I even went as far as wielding the wizard wand selection tool once. ...OK I'm rubbish when it comes to Photoshop.

Discussion

  1. The groupToMap() example might have an error – it’s still calling group()

    I didn’t know about these functions though, very useful!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!