group_by
function:
Group items by function. If the first argument is a list, return list grouped by the second argument.
Examples:
- running:
(group_by ["11", "5", "23", "ab", "1", "", "100", {}], (stringify (len .)))
will give:{"2": ["11", "23", "ab"], "1": ["5", "1"], "0": ["", {}], "3": ["100"]}
- running:
(group_by [{"g": "one", "v": 1}, {"g": "two", "v": 2}, {"g": "one", "v": 33}, {"g": "two", "v": false}], .g)
will give:{"one": [{"g": "one", "v": 1}, {"g": "one", "v": 33}], "two": [{"g": "two", "v": 2}, {"g": "two", "v": false}]}
- running:
(group_by [{"g": "one", "v": 1}, {"g": "two", "v": 2}, {"g": "one", "v": 33}, {"g": "two", "v": false}], (get . ^.key))
for input:{"key": "g"}
will give:{"one": [{"g": "one", "v": 1}, {"g": "one", "v": 33}], "two": [{"g": "two", "v": 2}, {"g": "two", "v": false}]}
Because this group the element by a key that is taken from the input ("key"
).
- running:
(group_by 344, (stringify (len .)))
will return nothing
- running:
(group_by ["11", "5", "23", "ab", "1", "", "100", {}], (len .))
will return nothing