How to combine many items in one array?

Good evening to all,
I’m calling on the community because I’ve spent more than 4 hours on this, testing different nodes, reading the n8n documentation to manipulate the data and reading the topics in the community. I haven’t found any solution :cry:

Here’s my problem: in the workflow I’m sharing just below I initialize quite a few variables (11 to be precise) that I would like to gather in an array in the node just after in order to output 1 single item. How can I do this? I managed to do it with the Node Set but it executes 11 times (which I absolutely do not want), even with “Execute Once” checked it does not work.

Can you help me?

Hi @arthurcorre

I might not understand correctly. But why are you not simply using one set to set all those values instead of separate SET nodes with each one field?

Because just before these many sets I make a forward API call for each (which I cannot unify unfortunately)

Ah ok,
Have you tried the merge node?
If everything needs to be set into one item you can use merge by index of multiplex.

Sorry yes I also tried with the Merge node trying different combinations, without success :confused:

You might need a function node for that one to manually merge all the items, If you didn’t want to take the javascript route though you could do it with a merge node like the below. To me though this feels like an ugly way to do it so it could be that there is another way to design your workflow maybe using sub workflows that can be called depending on what is being done unless you need to use all those variables at once.

Thank you for your reply, but unfortunately this is not the result I was expecting.

I’m copying the workflow I did again, this time with the node set properly configured after initializing my 11 different variables. The output of the last node set is exactly what I want as a result.

The problem is that it outputs 11 items and for the next node (Slack) there are 11 executions scheduled on this node, which I absolutely do not want. Would you know how to avoid this?

Hey @arthurcorre,

You can tell the Slack node to run only once by clicking on the settings and setting “Execute Once” which appears to have been done in your workflow.

What result are you expecting? I take it all 11 set nodes are not normally triggered in your workflow and it is only one at a time? Are you able to share the full workflow so we can get a better idea of what is happening.

Yes, I did check the “Execute once” setting for the last node Set as well as the one with Slack. I haven’t necessarily thought about the rest of the flow, but all the variables set will go into a Google Sheets database. I don’t think it matters much if I tell you what’s next in my workflow.

By doing the test with “Execute once” activated, I can confirm that I do receive 11 Slack messages… is this a bug? Should I share more information about my n8n instance with you so that you can better understand?

Could you please share a screenshot of the execution?
like @Jon hinted at, you are probably running all paths of the workflow at once. this makes the nodes at the end run 11 times. (when executing once it will run 11 times for 1 item not just 1 time in total)

Here is the result of the execution of my Slack node with the “Execute once” parameter checked.

I should point out that I have received 11 Slack messages, although I would like to receive only one, even though I have 11 items in input.

Yeah ok, so the node is triggered 11 times. and everytime it gets triggered it runs for one item with the Execute once option. This is all as intended.

You need to append the items like @Jon has shown to get all the items into 1 appended list. After you then use the execute once option, which will now work because you only have 1 trigger with 11 items and then it will run for the first item only.

Thank you for this new solution. Unfortunately it still does not meet my needs. As mentioned before, here is exactly what I need as input to update my Google Sheets database:

(The domain name being my ID for updating my database.)

Because using the Merge method (Append mode) mentioned by @Jon, this is what I get as output from my last Merge node:

All the data generated upstream must be stored at the same level, in an array. Without it, I cannot update my database in Google Sheets.

I would need to run the Google Sheets node (which I had replaced with Slack to simulate the action, and see how many times it actually runs) ONCE with this strict same input:

Is there a way to do this via Javascript? Because I have the impression that I have done the rounds of all the n8n native nodes, which obviously do not meet my very specific use case.

Hey @arthurcorre,

With the merge nodes we probably could have used another node to then sort the data out if we had that screenshot sooner.

The real quick solution would be to add a NoOp node that all the Set nodes go into then use a function node after that to merge all the items using something like…

const allData = []

let i = 0;
do {
  try {
    // Replace NoOp with the name of your NoOp node
    allData.push.apply(allData, $items("NoOp", 0, i));
  } catch (error) {
    return allData;  
} while(true);
1 Like

Thank you Jon again for your time.

Having installed a NoOp node just after the 11 Set nodes and a Function node just after with the code snippet you shared just before, this is what I get:

Unfortunately, this does not meet my needs. Moreover, the node Function outputs 121 items when I need only ONE:

Does execute once change anything? Now I can see a bit more of your workflow I can work on trying to create a smaller version to experiment with.

I see this is an older post, but I am experiencing a similar issue.

I call on a google sheet and get (for example) 10 lines. I then send this through an if node towards slack

Google sheets → if node (if cell equals ___) → slack message (column 1 from each line)

I would prefer if this were all sent as a single message to slack, a grouped message containing each column 1 from each line.

I am curious if you figured this out, @arthurcorre

Hi n1isaac,

you could use “Item Lists” node to concatenate (aggregate) all items into a new field. Then you get all items into one item in one array, pls. see screenshot.

  • Kurt
1 Like