JavaScript Array Group

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 […]

The post JavaScript Array Group appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

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.

The post JavaScript Array Group appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2023-04-17T09:08:26+00:00) JavaScript Array Group. Retrieved from https://www.scien.cx/2023/04/17/javascript-array-group/

MLA
" » JavaScript Array Group." David Walsh | Sciencx - Monday April 17, 2023, https://www.scien.cx/2023/04/17/javascript-array-group/
HARVARD
David Walsh | Sciencx Monday April 17, 2023 » JavaScript Array Group., viewed ,<https://www.scien.cx/2023/04/17/javascript-array-group/>
VANCOUVER
David Walsh | Sciencx - » JavaScript Array Group. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/17/javascript-array-group/
CHICAGO
" » JavaScript Array Group." David Walsh | Sciencx - Accessed . https://www.scien.cx/2023/04/17/javascript-array-group/
IEEE
" » JavaScript Array Group." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2023/04/17/javascript-array-group/. [Accessed: ]
rf:citation
» JavaScript Array Group | David Walsh | Sciencx | https://www.scien.cx/2023/04/17/javascript-array-group/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.