Sum values with two keys

Hello, I’m trying to replicate the solution from this topic ( Sum values with the same key), but I have two keys and can’t find out how to do that especially with this new Code node :frowning:
Here is my data

[
{
"distance":20.0206999999937,
"truck_no":"12",
"jurisdiction":"AB",
"id":498645
},
{
"distance":305.152699999977,
"truck_no":"02",
"jurisdiction":"AB",
"id":498637
},
{
"distance":270.225199999986,
"truck_no":"02",
"jurisdiction":"SK",
"id":498637
},
{
"distance":513.655900000012,
"truck_no":"02",
"jurisdiction":"SK",
"id":498637
},
{
"distance":618.968100000056,
"truck_no":"12",
"jurisdiction":"ND",
"id":498645
},

]

What I’m trying to achieve is get this structure

{
"distance": sum of all jurisdictions distance,
"truck_no": "02",
"id": 498637
}
{
"distance": sum of all jurisdictions distance,
"truck_no": "12",
"id": 498645
}

Thank you

Hello @Ruslan_Yanyshyn !

You can indeed use the code node and map through all the data and sum the distances using a reduce.

Here is the workflow:



Here is the code:

const data = [{
        "distance": 20.0206999999937,
        "truck_no": "12",
        "jurisdiction": "AB",
        "id": 498645
    },
    {
        "distance": 305.152699999977,
        "truck_no": "02",
        "jurisdiction": "AB",
        "id": 498637
    },
    {
        "distance": 270.225199999986,
        "truck_no": "02",
        "jurisdiction": "SK",
        "id": 498637
    },
    {
        "distance": 513.655900000012,
        "truck_no": "02",
        "jurisdiction": "SK",
        "id": 498637
    },
    {
        "distance": 618.968100000056,
        "truck_no": "12",
        "jurisdiction": "ND",
        "id": 498645
    },
];

const transformedDataObj = data.reduce((acc, current) => {
    if (!acc[current.id]) {
        acc[current.id] = {
            distance: current.distance,
            truck_no: current.truck_no,
            id: current.id
        };
    } else {
        acc[current.id].distance += current.distance;
    }
    return acc;
}, {});

const transformedData = Object.values(transformedDataObj).map(el => ({
    json: el
}));

return transformedData;


And here is the output:

[
  	{
        "distance": 1089.0337999999751,
        "truck_no": "02",
        "id": 498637
    },
    {
        "distance": 638.9888000000497,
        "truck_no": "12",
        "id": 498645
    }
]


Don’t hesitate if you have any question :blush:
Hope it helps!

1 Like

Thank you very much! @eldala07 I know it should work perfectly but with new Code node it has much harder to find data, I hate this new Code especially with lack of documentation and examples. So I’m getting this if use $input.all() or items as data source.


and here with console log

Hey @Ruslan_Yanyshyn,

Give the workflow below a go, It should output something that looks like this.
image

Thank you @Jon it indeed works, but when I tried find data by myself I had problem. Usually when you put dot in the end of function or value you can see what is next in the structure but in this case I couldn’t see that. When for example I put the dot after current. (in this particulary case) I couldn’t see the following json and so on.

And the second question is how to deal with pagination. I would like to add data to the same output every iteration. Could you please tel me how to achieve that?
Thank you in advance.

Hey @Ruslan_Yanyshyn,

Auto complete is an interesting one and doesn’t always work on variables which can make it a bit tricky, You can always type out the data manually though which isn’t so bad once you know how the data structure works.

Pagination would depend on the API you are using, What I like to do is wait for all the runs to complete then merge all the data and work with it from there. You can find an example on merging execution runs here: Merge multiple runs into one | n8n workflow template

Hey @Jon Thank you. I saw that solution but isn’t what I’m trying to achieve.
After each iteration I got sum of distance for particular “id” and I would like to continue that for the next page because it may contain the same “id” so I need somehow sum distance again. But I don’t need simply concatenate it in one list which will contains duplicate id’s.
It would be great if the Code node will do the same for each page and then sum the distance for particular “id” like it does for one iteration.

Hey @Ruslan_Yanyshyn,

In that case I don’t think I fully understand what you are trying to do, Merging the execution runs then running the code node to do the maths should provide what you are after as it would loop through every item in one hit.

Sorry @Jon, my fault, I mixed it up at first. I put the nodes in the wrong sequence. Now everything works like clockwork, thanks a lot for your help.
I think it would be a good idea to create a “Transformation” node that would allow all operations to be done interactively rather than using programming skills.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.