Calculate Average of an array of numbers

Hello n8n Family,
I am trying to calculate the average of an array of numbers,
here is my input:
[

{

“rates”:[

1.32775,

1.331735,

1.34095,

1.34105,

1.340035,

1.34435,

1.339125,

1.344425,

1.344935,

1.33875,

1.33885,

1.335665,

1.333455,

1.33396

]

}

]

I need output like average: 1.3456
I tried several examples of the for-each loop here from different posts but couldn’t get it to work.
Help please

Hi @intelioum!

I’m assuming you’re on one of the latest versions, to achieve this you could use an expression to call one of our in-built functions on the rates array — average.


And here’s an example workflow, just please keep in mind that in my example I pinned the array data on the manual node, so you’d need to do the same or get the data as input from some other node. Data pinning is really useful when building n8n workflows you can check more about them here.

Hope that helps!

Oleg

3 Likes

Hi Oleg,
Thank you very much, that worked like a charm

1 Like

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