{"id":8315,"date":"2022-11-28T20:58:00","date_gmt":"2022-11-29T01:58:00","guid":{"rendered":"https:\/\/www.ericfeminella.com\/blog\/?p=8315"},"modified":"2024-03-09T23:17:49","modified_gmt":"2024-03-10T04:17:49","slug":"native-data-categorization-with-object-groupby","status":"publish","type":"post","link":"https:\/\/www.ericfeminella.com\/blog\/2022\/11\/28\/native-data-categorization-with-object-groupby\/","title":{"rendered":"Native Data Categorization with Object.groupBy"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/img\/articles\/object-groupby.png\"\/><\/p>\n<p>The introduction of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Object\/groupBy\" rel=\"noopener noreferrer\" target=\"_blank\">Object.groupBy<\/a> allows for a streamlined, native approach to organizing collections of data based on user defined criterion; thus simplifying the task of data analysis and categorization without the need for managing third-party dependencies.<\/p>\n<h2 id=\"using-object-groupby\" class=\"section-link\">\n  <a href=\"#using-object-groupby\">Using Object.groupBy<\/a><br \/>\n<\/h2>\n<p>Using <code>Object.groupBy<\/code> is simple and straight-forward. If you have previously used <a href=\"https:\/\/lodash.com\/docs\/#groupBy\" target=\"_blank\" rel=\"noopener\">Lodash groupBy<\/a>, then you are already familiar with it&#8217;s API. <code>Object.groupBy<\/code> accepts an array and a callback function which defines the grouping logic, and returns an object of groupings based on the callback function&#8217;s returned <code>key<\/code>.<\/p>\n<p>For example, we can group employees by department as follows:<\/p>\n<p id=\"example-1\" class=\"example-link\">\n  <a href=\"#example-1\">Example 1<\/a>\n<\/p>\n<pre class=\"lang:js decode:true\">\nconst employees = [\n  {id: 123, department: 'Engineering', name: 'John Doe', doh: '12\/22\/20'},\n  {id: 321, department: 'Product', name: 'Jane Smith', doh: '06\/12\/19'},\n  {id: 213, department: 'Product', name: 'Fred Jones', doh: '03\/13\/12'},\n  {id: 312, department: 'Engineering', name: 'Kim Tyler', doh: '12\/19\/21'},\n];\nconst departments = Object.groupBy(employees,({department}) => department);\nconsole.log(departments);\n\/\/ {\n\/\/   Engineering: [\n\/\/    {id: 123, department: 'Engineering', name: 'John Doe', ...},\n\/\/    {id: 312, department: 'Engineering', name: 'Kim Tyler', ...}\n\/\/   ],\n\/\/   Product: [\n\/\/     {id: 321, department: 'Product', name: 'Jane Smith', ...},\n\/\/     {id: 213, department: 'Product', name: 'Fred Jones', ...}\n\/\/   ]\n\/\/ }\n<\/pre>\n<p>In the above example, we see that the provided array can easily be grouped into specific properties, in this case, by department. We can just as easily have grouped the array by any other property as well, such as date of hire (doh) to categories by employee tenure (<em><a href=\"#example-3\">more on this shortly<\/a><\/em> ). <\/p>\n<p>Indeed, <code>Object.groupBy<\/code> is particularly useful for grouping collections of objects; however, it is not restricted to objects alone, it can also be used to create grouping primitives as well: <\/p>\n<p id=\"example-2\" class=\"example-link\">\n  <a href=\"#example-2\">Example 2<\/a>\n<\/p>\n<pre class=\"lang:js decode:true\">\nconst values = [true, false, true, true, false];\nconst {enabled, disabled} = Object.groupBy(\n  values, value => value ? 'enabled' : 'disabled'\n);\nconsole.log(enabled.length); \/\/ 3\nconsole.log(disabled.length); \/\/ 2\n<\/pre>\n<p>While the above examples are useful in their own right, the real power of <code>Object.groupBy<\/code> is revealed when more complex logic is required for determining groupings. For example, we can group the <code>employees<\/code> array by tenure as follows: <\/p>\n<p id=\"example-3\" class=\"example-link\">\n  <a href=\"#example-3\">Example 3<\/a>\n<\/p>\n<pre class=\"lang:js decode:true\">\nconst employees = [\n  {id: 123, department: 'Engineering', name: 'John Doe', doh: '12\/22\/20'},\n  {id: 321, department: 'Product', name: 'Jane Smith', doh: '06\/12\/19'},\n  {id: 213, department: 'Product', name: 'Fred Jones', doh: '03\/13\/12'},\n  {id: 312, department: 'Engineering', name: 'Kim Tyler', doh: '12\/19\/21'},\n];\n\nconst groupByTenure = (employees, target) => {\n  const year = (1000 * 60 * 60 * 24 * 365.25);\n  return Object.groupBy(employees,({doh}) => {\n    const tenure = (target - new Date(doh)) \/ year;\n    return (\n      tenure > 1 ? (\n        tenure <= 2 ? 'development' : 'retention'\n      ) : 'onboarding'\n    )\n  })\n};\n\nconsole.log(\n  groupByTenure(employees, new Date('11\/28\/22'))\n);\n\/\/ {\n\/\/   \"development\": [{\n\/\/     \"id\": 123,\n\/\/     \"department\": \"Engineering\",\n\/\/     \"name\": \"John Doe\",\n\/\/     \"doh\": \"12\/22\/20\"\n\/\/   }],\n\/\/   \"retention\": [{\n\/\/     \"id\": 321,\n\/\/     \"department\": \"Product\",\n\/\/     \"name\": \"Jane Smith\",\n\/\/     \"doh\": \"06\/12\/19\"\n\/\/   }, {\n\/\/     \"id\": 213,\n\/\/     \"department\": \"Product\",\n\/\/     \"name\": \"Fred Jones\",\n\/\/     \"doh\": \"03\/13\/12\"\n\/\/   }],\n\/\/   \"onboarding\": [{\n\/\/     \"id\": 312,\n\/\/     \"department\": \"Engineering\",\n\/\/     \"name\": \"Kim Tyler\",\n\/\/     \"doh\": \"12\/19\/21\"\n\/\/   }]\n\/\/ }\n<\/pre>\n<h2 id=\"concluding-thoughts\" class=\"section-link\">\n  <a href=\"#concluding-thoughts\">Concluding Thoughts<\/a><br \/>\n<\/h2>\n<p>New features such as `Object.groupBy` serve to highlight the <a href=\"https:\/\/ecma-international.org\/technical-committees\/tc39\/\" target=\"_blank\" rel=\"noopener\">TC39 Committee's<\/a> commitment to providing developers with powerful tools which simplify common tasks. By introducing a native facility for grouping objects, <code>Object.groupBy<\/code> simplifies overhead and maintainability while also opening up new opportunities for native data aggregation and analysis.<\/p>\n<p><em>Update: <date>November 28, 2023<\/date><\/em>: <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Object\/groupBy\" rel=\"noopener noreferrer\" target=\"_blank\">Object.groupBy<\/a> is now currently in <a href=\"https:\/\/github.com\/tc39\/ecma262\/pull\/3176\" target=\"_blank\" rel=\"noopener\">Stage 4 status<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The introduction of Object.groupBy allows for a streamlined, native approach to organizing collections of data based on user defined criterion; thus simplifying the task of data analysis and categorization without the need for managing third-party dependencies. Using Object.groupBy Using Object.groupBy is simple and straight-forward. If you have previously used Lodash groupBy, then you are already familiar with it&#8217;s API. Object.groupBy&#8230; <a class=\"read-more\" href=\"https:\/\/www.ericfeminella.com\/blog\/2022\/11\/28\/native-data-categorization-with-object-groupby\/\">Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":8378,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[42,246,78,45],"tags":[261,68,364,365],"class_list":["post-8315","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apis","category-es6","category-javascript-2","category-refactoring","tag-es6","tag-javascript","tag-object-groupby","tag-tc39"],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.ericfeminella.com\/blog\/wp-content\/uploads\/2018\/07\/object-groupby.png?fit=718%2C320&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/8315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/comments?post=8315"}],"version-history":[{"count":0,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/8315\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/media\/8378"}],"wp:attachment":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/media?parent=8315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/categories?post=8315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/tags?post=8315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}