Getting the average of multiple Items

Hello

How can I get the average of all the “play_counts” for each person? I’m currently using the summarize node but it only outputs 1 average so it bottlenecks my flow.

Here is the output for the http request: https://filebin.net/enhsmdsg5tv9hevp

Current output:
Person 1:
play_count[1]: 5
play_count[2]: 10
play_count[3]: 15
etc… Up to 20 play_counts

Person 2:
play_count[1]: 6
play_count[2]: 14
play_count[3]: 1
etc… Up to 20 play_counts

Person 3:
play_count[1]: 8
play_count[2]: 3
play_count[3]: 7
etc… Up to 20 play_counts

Desired output:

Person 1:
Average: 10

Person 2:
Average: 7

Person 3:
Average: 6

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

@Trash ,

1 Like

thanks for the reply

the number of people/items that I want the average of is dynamic so I don’t think that would work. Im currently using the summarize node but I can only pass 1 person/item through it at a time

@Trash , why is it not going to work? It should work no matter the number of people and the play count. It has to work.

1 Like

I guess what I’m asking for is if you could elaborate a little more on how that would work. Sorry if the data wasn’t clear enough, here is the workflow with the data.

Here is the data that is returned by the http request: Filebin | enhsmdsg5tv9hevp

I also don’t know any javascript by the way, so the extent of my ability is to copy and paste the javascript into the code node.

again thanks for your help.

@Trash , thanks for showing the actual data. This calls for some adjustments but it still works the same way as I showed before. The additional step requires utilization of JMESPath as your data is in the following format (much simplified leaving only what matters for this task).

[
  // person 1
  {
    "aweme_list": [
      {
        "author_user_id": ABC,
        // play count 1
        "statistics": {
          "play_count": 123
        }
      },
      // play count 2
      {
        "author_user_id": ABC,
        "statistics": {
          "play_count": 456
        }
      }
    ]
  },
  // person 2
  {
    "aweme_list": [
      {
        "author_user_id": XYZ,
        "statistics": {
          "play_count": 789
        }
      },
      {
        "author_user_id": XYZ,
        "statistics": {
          "play_count": 135
        }
      }
    ]
  }
]

Therefore, to get hold of the persons and the list of their play counts, I use an extra Set utilizing JMESPath expressions:

  • {{ $jmespath($json.aweme_list, "[0].author_user_id") }} to get the person (user ID) - you can pick a different person attribute
  • {{ $jmespath($json.aweme_list, "[*].statistics.play_count") }} to get the array of all the play counts for that user

This is done to that the initial solution I proposed can work without any changes. Thus, the final workflow is like this

2 Likes

Thanks Alot

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