I'm a Ruby on Rails and React Native enthusiast with over four years of experience creating innovative software solutions. I work well in teams and am always up for challenging projects. I enjoy playing chess and gaming in my free time.
3 minute read
As JavaScript,
being one of the most dynamic
and widely-used programming languages,
constantly evolves to meet the needs of developers.
JavaScript has introducd Object.groupBy
and
Map.groupBy for grouping data in the latest
proposal
that has reached stage 4,
Before
Prior to the introduction of Object.groupBy() in JavaScript,
array elements were normally grouped using a combination of
Array.reduce()
and
other array manipulation techniques.
Here is an example of how we can organize the timesheetEntries array based on the
‘project’
property:
Object.groupBy
The Object.groupBy method efficiently groups items based on an arbitrary key.
As demonstrated in the below example,
we can effortlessly group elements by applying a custom logic,
such as odd/even numbers.
The result is a null-prototype object,
enhancing destructuring
and
preventing accidental collisions with global Object properties.
Map.groupBy:
Map.groupBy returns items in a Map,
providing a more versatile approach for grouping using object keys.
This is particularly useful for scenarios involving complex key types,
such as compound keys
or
tuples.