groupBy API
To work with nested data in tidy, you use the groupBy, which runs a subflow of tidy functions on nested groups of the data and can be exported into different forms.
groupBy#
Restructures the data to be nested by the specified group keys then runs a tidy flow on each of the leaf sets. Grouped data can be exported into different shapes via group export helpers, or if not specified, will be ungrouped back to a flat list of items.
Parameters#
groupKeys#
Either a key in the item or an accessor function that returns the grouping value, or an array combining these two options.
Note that the grouping logic uses strict equality === to see if keys are the same, so if you're using non-primitive values that are equal, but not identical (e.g. ["foo", "bar"] !== ["foo", "bar"]), they won't be grouped together. In those cases, the current recommendation is specifying groupKeys as a function that returns a primitive value (e.g. (item) => item.arrKey.join('---')).
fns#
A tidy subflow: an array of tidy functions to run on the leaf sets of the grouped data.
options?#
Options to configure how groupBy operates:
addGroupKeys = true: Whether to merge group keys back into the objects.
Usage#
Group Exports#
The final argument to groupBy can also be an export function:
- groupBy.entries(options?)
- groupBy.entriesObject(options?)
- groupBy.grouped(options?)
- groupBy.keys(options?)
- groupBy.map(options?)
- groupBy.object(options?)
- groupBy.values(options?)
The levels export allows combining any of the above at different depths of the tree:
- groupBy.levels(options?)
Group Export Options#
These functions take options as their argument, which includes the options mentioned above plus some extras specific to exporting:
export = 'ungrouped': Specifies how the data should be exported from the groupBy function. If anything besides nully or "ungrouped", the remaining options will be interpreted to inform the output.flat?: if all nested levels should be brought to a single top levelcompositeKey?: when flat is true, how to flatten nested keys (default joins with"/")single?: whether the leaf sets consist of just one item (typical after summarize). if true, uses the first element in the leaf set instead of an arraymapLeaf?: operation called on each leaf during export to map it to a different value (default: identity)mapLeaves?: operation called on each leaf set to map the array of values to a different value. Similar torollupfrom d3-collection nest or d3-array (default: identity)mapEntry?: when export = "entries" only operation called on entries to map from [key, values] to whatever the output of this is (e.g.{ key, values }) (default: identity)levels?: required when export = "levels" only specifies the export operation for each level of the grouping
groupBy.entries()#
Exports the data as entries arrays where the keys are the value of the key for that group level and the values are either nested entries or a flat list of items if it is a leaf set.
groupBy.entriesObject()#
Exports the data as entries objects { key: string, values: any[] } where the keys are the value of the key for that group level and the values are either nested entries or a flat list of items if it is a leaf set.
groupBy.grouped()#
Exports the data as a Grouped Map where the keys are tuples [keyName, keyValue] and the values are either nested Grouped Maps or a flat list of items if it is a leaf set. Note this is similar to groupBy.map, but it uses a tuple for the keys.
groupBy.keys()#
Exports the data as keys arrays, which are similar to entries except they do not include any of the values. Is this useful? Hard to know.
groupBy.map()#
Exports the data as Map objects where the keys are the value of the key for that group level and the values are either nested Map objects or a flat list of items if it is a leaf set. Note this is similar to groupBy.grouped, but it doesn't use a tuple for keys.
groupBy.object()#
Exports the data as objects where the keys are the value of the key for that group level and the values are either nested objects or a flat list of items if it is a leaf set.
groupBy.values()#
Exports the data as values arrays which are similar to entries arrays except they contain no keys. Note if you are just trying to get the values back as a single flat list, you do no need to use any export method as that is the default behavior ("ungrouped").
groupBy.levels()#
Exports the data in a different way for each level. The last level specified is used for all remaining levels. You must supply a value for levels in the options argument.
Custom Levels Export#
For a more advanced export, a custom levels export can be specified by providing a LevelSpec for the value of levels:
Probably best to just look at the source code of the existing groupBy methods to get an idea of how to use this.