How to sum all values from an array

How to sum all values from an array in Make.com?

Hi everyone,

I’m trying to sum all viewCount values from YouTube API response but having issues.

My setup:

  • YouTube Videos API returns 14 video items
  • Each item has statistics.viewCount (like 12675, 25000, etc.)
  • I want to sum all these viewCount values

What I tried:

  • {{sum(18.items[].statistics.viewCount)}} → Returns only first value
  • {{add(18.items[].statistics.viewCount)}} → Error
  • {{length(18.items)}} → Returns 14 (correct)
  • {{18.items[1].statistics.viewCount}} → Returns 12675 (first video’s views)

Expected result: Total sum of all 14 videos’ viewCount
Actual result: Only getting the first video’s viewCount

What’s the correct syntax to sum all array values in Make.com? :sob::sob:

1 Like

Welcome to the Make community!

You’ll need to use the map function to extract all the viewCount properties from the array of collections, before you can use sum.

To do this, you can use the built-in function map — “Returns a primitive array containing values of a complex array. Allows filtering values. Use raw variable names for keys.”

{{ map (complex array; key; [key for filtering]; [possible values for filtering separated by a comma]) }}

After that, you will be able to use the sum function — “Returns the sum of the values in a specified array, or the sum of numbers entered individually.”

{{ sum ([array of values]), sum(value1;value2; ...) }}

Answer

Here are both functions combined, using the variables you provided:

{{ sum(map(18.items; statistics.viewCount)) }}

(copy-paste the above into the field, or type it exactly as shown)

For more information, please refer to these courses in the Make Academy:

Using get () and map () functions

  • The get() function
  • The map() function
  • The get() and map() functions
  • The get(map()) use case

Hope this helps! Let me know if there are any further questions or issues. P.S.: investing some effort into the tutorials in the Make Academy will save you lots of time and frustration using Make!

@samliew

Hi @hanabi_6,

I don’t believe the Sum function can work here.

What I think is a better approach is initializing a variable called “Total Count” with a value of 0.

Iterate on the array of Youtube videos and with each iteration append views to the Total Count variable.

I’d like to provide more detail but I don’t have much information to provide the exact approach.

Hope this helps!

-@Trainward_Consulting

That’s exactly what the sum() function is for - to sum up an array of values. How would it not work here?