{
"id": "fn-array-group_by",
"dataComponent": "array",
"heading": {
"title": "group_by",
"badges": ["Array", "PARTIAL"]
},
"synopsis": "Groups elements of an array by the result of a function called on each element. Returns a keyed array of grouped subarrays. Partial and placeholder usage supported.",
"codeBlocks": [
"extend(\"array\")\n\narr = [ [type:\"cat\", name:\"Milo\"], [type:\"dog\", name:\"Rex\"], [type:\"cat\", name:\"Kit\"] ]\ngrouped = array:group_by(obj => array:prop(\"type\", obj), arr)\nsput(grouped[\"cat\"])\n# => [ [type:\"cat\", name:\"Milo\"], [type:\"cat\", name:\"Kit\"] ]\nsput(grouped[\"dog\"])\n# => [ [type:\"dog\", name:\"Rex\"] ]\n\n# Partial usage:\ngb = array:group_by(obj => array:prop(\"type\", obj))\nres = gb(arr)\nsput(res[\"cat\"][1][\"name\"])\n# => \"Kit\"\n\n# Placeholder usage:\ngbp = array:group_by(_, arr)\nsput(type(gbp(obj => array:prop(\"type\", obj))))\n# => \"keyed_array\""
],
"notes": [
"First argument: a function mapping each element to a key (string/int/float).",
"Second argument: MixedArray to group.",
"Result is a keyed array mapping group key to array of elements.",
"Partial and placeholder usage is supported.",
"If function does not return a valid key, group key is '_'."
]
}