How to add key/value to existing multiple items object

Describe the issue/error/question

Using Function node to extract the object with multipe objects which i can then push to create multiple rows on nodes like Baserow or Google Sheet.

However I need to add in some header key/value so that it can be populate in each items.

I placed a Set node after the Function node to grab the value from 2 nodes away (before Function node) but only populate in the first item.

Can point to which node to use to add in the key/value to each items?

Thanks

I placed a Set node after the Function node to grab the value from 2 nodes away (before Function node) but only populate in the first item.

Hi @leonardchiu, the Set node should let you add a key value pair to all items, not just the first. Check out this example:

In this example, the key foo with a value of bar is added to all items:

Do you perhaps have a different data structure? Or have the Execute Once setting highlighted below enabled?

image

Thanks @MutedJam for responding.

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

[
{
header: {
key1: value1
},
object1: [
{
key2: value2,
key3: value3
},
{
key4: value4,
key5: value5
}
],
}
]

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.

1 Like

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.

Thanks

1 Like

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.

1 Like

Hi @MutedJam, thanks - I was wondering the same thing and $item(0) worked like a charm.

However, I also tried .first() and n8n throws an error then:
ERROR: first() is only callable on type “Array”

It’s not really an issue since $item(0) works. Just curious. :slight_smile:
I’m on 0.219.1

Cheers!