Now i understand that if the value is fixed or directly from the previous node by expression, it will be inserted into every item. However my workflow can only get the value from 1 node away because I need the function node to extract the object1 from main object.
I need to include key1: value1 in all the items
I tested with fixed value and previous node it works. Set node after the function node grabing value from node before this function wouldnt. I think because the expression is grabing from has only 1 object hence subsequent items couldn’t get any value. Any advise?
I modify your example and wanted to add a fixed “US” to all items but instead got the different country from all 5 items from the previous node.
What i wanted to do was to save a variable and insert to all items. It there any way i can set a variable like traditional programming?
What i wanted to do was to save a variable and insert to all items. It there any way i can set a variable like traditional programming?
So n8n doesn’t have “global” variables, but you could make sure to set a single value to all items. Here’s your example slightly modified with an added “Variable” Set node containing a single value. This value will then be added to all items in your existing Set node:
This uses pretty much your existing expression, only with an updated node name and $item(0). in front of it. The $item(0). prefix will cause n8n to always read the first item from the Variable node.
This solve the problem perfectly. But i probably wouldn’t be able to figure out myself. Probably need to work on n8n more to deeply understand more.
So by prefixing $item(0). on the expression it will not loop through to grab any subsequent value despite there is no other items? Really got to wrap my head around on how n8n works.
But support here is really good. I shall plan to sub on a cloud tier.
Exactly, $item(0). will cause n8n to always use the first item from another node. By default, n8n would match the item as the respective index for an expression. So for the first item arriving at the Set node, n8n would read the first item from the “Variable” node (or whatever other node you specify). For the second item arriving at the Set node it would use the second item from the other node and so on.
$item(0). is btw somewhat outdated, expressions have been simplified a bit recently. So if you are using a recent version of n8n you could alternatively use {{ $("Variable").first().json["foo"] }} instead. The .first() syntax is documented here.