How to append an array from split-out node into another array

I am batching requests to an API which returns a field containing array. I’m using split-out to parse out array and append to an existing variable. However, I am getting an odd result that seems to create an array of arrays. I was able to find a workaround with the Code node, but the behavior from split-out to set node seems odd. Ideally I want to avoid using Code node as I read that it is a memory intensive node.

Share the output returned by the last node

Information on your n8n setup

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

Hey @jtoh , you have different datasets produced by Split Out node and Code nodes. You cannot expect the same result if your data input is different.

If you want the result as in Code node then you do not need Split Out node as its role is to take the items out of an array (which is not what you do in Code node).

Insted of Split Out, use Set node

Yes, so I tried using the Set node alone. But I could not figure out why it wasn’t appending to the existing array. Here is the updated workflow to demonstrate.

Because with each loop you run a different iteration independent of the previous one. If you want to refer to a previous iteration you need to engage $runIndex variable. See Output of other nodes | n8n Docs.

i don’t understand. If this is my set expression:

{{ $('Create vars').item.json.final_results.append($json.data) }}

then this means, by the 2nd iteration, final_results should already contain the previous $json.data. This should just append the new $json.data to it shouldn’t it?

I don’t understand where $runIndex would be applicable here since that doc shows it applies to all, first and last methods, which i’m not using.

Hey @jtoh , I think I misunderstood. The problem with your append is you are appending arrays to the array in “Create vars”. I believe you want to append items (not array of items) to the array. For that, you need to replace append with concat as below:

{{ $('Create vars').item.json.final_results.concat($json.data) }}

See JavaScript Array concat() Method for details on the function.

Yup, its funny I actually realized that too. But unfortunately, the behavior is still the same. I pinned the data for you to see. It must be something up with that set node because i’m seeing the correct data from the input window.

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