How to calc sum of nested arrays with group by

Hi,

I'm new to node-red and have a question regarding how to calculate the sum with group by of a nested array.

The outcome I would like to have is (the sum of of amount of the payments grouped by type):
[
{"type":"card",
"'amount":1231.15
},
{"type":"cash",
"'amount":99.15
}
]

You can try this flow:

[
    {
        "id": "061b7f51de6e3f24",
        "type": "inject",
        "z": "a442b54b44edc349",
        "name": "",
        "props": [
            {
                "p": "payload.payments",
                "v": "[{\"type\":\"card\",\"amount\":1231.15},{\"type\":\"cash\",\"amount\":99.15},{\"type\":\"card\",\"amount\":1231.15},{\"type\":\"cash\",\"amount\":99.15}]",
                "vt": "json"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 190,
        "y": 140,
        "wires": [
            [
                "58684a2daf702d68"
            ]
        ]
    },
    {
        "id": "3617d84d9f3673eb",
        "type": "split",
        "z": "a442b54b44edc349",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 550,
        "y": 140,
        "wires": [
            [
                "6f49662d80bcc2c5"
            ]
        ]
    },
    {
        "id": "3d43b37bc60ef510",
        "type": "debug",
        "z": "a442b54b44edc349",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1250,
        "y": 140,
        "wires": []
    },
    {
        "id": "6830e799f0c7d768",
        "type": "join",
        "z": "a442b54b44edc349",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": true,
        "timeout": "",
        "count": "1",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 870,
        "y": 140,
        "wires": [
            [
                "898ba01ab384793d"
            ]
        ]
    },
    {
        "id": "58684a2daf702d68",
        "type": "change",
        "z": "a442b54b44edc349",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.payments",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 370,
        "y": 140,
        "wires": [
            [
                "3617d84d9f3673eb"
            ]
        ]
    },
    {
        "id": "6f49662d80bcc2c5",
        "type": "function",
        "z": "a442b54b44edc349",
        "name": "",
        "func": "var sum = context.get(msg.payload.type)  || 0;\nsum = sum + msg.payload.amount;\ncontext.set(msg.payload.type,sum);\nmsg.topic = msg.payload.type;\nmsg.payload = sum;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 700,
        "y": 140,
        "wires": [
            [
                "6830e799f0c7d768"
            ]
        ]
    },
    {
        "id": "c5d25f23090950e9",
        "type": "inject",
        "z": "a442b54b44edc349",
        "name": "reset",
        "props": [
            {
                "p": "reset",
                "v": "true",
                "vt": "bool"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 690,
        "y": 200,
        "wires": [
            [
                "6830e799f0c7d768"
            ]
        ]
    },
    {
        "id": "898ba01ab384793d",
        "type": "trigger",
        "z": "a442b54b44edc349",
        "name": "",
        "op1": "",
        "op2": "",
        "op1type": "nul",
        "op2type": "payl",
        "duration": "250",
        "extend": true,
        "overrideDelay": false,
        "units": "ms",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 1040,
        "y": 140,
        "wires": [
            [
                "3d43b37bc60ef510"
            ]
        ]
    }
]

For sure you can calculate everything within the function node, but this gives in my opinion more an idea how to use the different nodes. :wink: