-
Sometimes it's useful to keep track of individual items after summarizing. For example, if we want to go from this input: [
{ "group": "a", "name": "n0", "value": 5 },
{ "group": "a", "name": "n1", "value": 10 },
{ "group": "b", "name": "n2", "value": 12 },
{ "group": "c", "name": "n3", "value": 35 },
{ "group": "c", "name": "n4", "value": 55 }
] To this output: [
{
"group": "a",
"total": 15,
"items": [
{"group": "a", "name": "n0", "value": 5},
{"group": "a", "name": "n1", "value": 10}
]
},
{
"group": "b",
"total": 12,
"items": [{"group": "b", "name": "n2", "value": 12}]
},
{
"group": "c",
"total": 90,
"items": [
{"group": "c", "name": "n3", "value": 35},
{"group": "c", "name": "n4", "value": 55}
]
}
] Is it possible to do this with summarize? |
Beta Was this translation helpful? Give feedback.
Answered by
pbeshai
Feb 5, 2021
Replies: 1 comment
-
This can be accomplished by adding an identity summarizer: For example: tidy(
data,
groupBy(
['group'],
[summarize({ total: sum('value'), items: (items) => items })]
)
) Try it out here on Observable |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
pbeshai
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This can be accomplished by adding an identity summarizer:
items => items
, which adds a key with the array of grouped items as its value.For example:
Try it out here on Observable