Array.groupBy(key) Last updated: 22. Jul 2024

Groups the items in an array based on the specified property name or function.

Parameters

Name Type Description
key string | function A property name or a function that returns the key to group by.

Returns

An object where the keys are the group names and the values are arrays of items in those groups.

Example

Code example (JS)

JS is normal JavaScript either running in the browser or on the Docly™ server.
const words = ["abcdefg", "efgha", "abc123"];
const groupedByFirstLetter = words.groupBy(word => word[0]);
write(JSON.stringify(groupedByFirstLetter));

Output

The JS code above produces the output shown below:
{"a":["abcdefg","abc123"],"e":["efgha"]}