# Function Node Output Data

**URL:** <https://community.n8n.io/t/function-node-output-data/8988>\
**Category:** Questions\
**Tags:** data-transformation, node\
**Created:** [November 8, 2021, 12:51pm UTC](https://community.n8n.io/t/function-node-output-data/8988 "2021-11-08T12:51:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mulen](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@Mulen](https://community.n8n.io/u/Mulen)\
**Post date:** [November 8, 2021, 12:51pm UTC](https://community.n8n.io/t/function-node-output-data/8988/1 "2021-11-08T12:51:32Z")

</div>

Hello.

Can someone pinpoint me what am i missing in the following scenario?

I have a javascript code that runs correctly.

> **[Run JavaScript in the browser - glot.io](https://glot.io/new/javascript)**
>
> Run JavaScript online in the browser. No installation required.

```auto
let j=[
{
"DirName": "mour"
},
{
"DirName": "dmyk"
},
{
"DirName": "oiad"
},
{
"DirName": "deko"
},
{
"DirName": "dvia"
}
];

let values=j.reduce((o,a)=>{
                    let ini=[];
                    ini.push("(", "NULL", 5, a.DirName, "NOW", ")");
                    o.push(ini);
                    return o;
              },[]);
              
let forInsert = values.join(', ');
let finalInsert = forInsert.replace(/\(\,/g, "(").replace(/\,\)/g, ")");

console.log(finalInsert);

```

The result is

```auto
(NULL,5,mour,NOW), (NULL,5,dmyk,NOW), (NULL,5,oiad,NOW), (NULL,5,deko,NOW), (NULL,5,dvia,NOW)

```

I want to replicate it in n8n that but i think i am missing something minor and i get stuck.

The code is

If you run it the result is

```auto
(NULL,5,,NOW), (NULL,5,,NOW), (NULL,5,,NOW), (NULL,5,,NOW), (NULL,5,,NOW)

```

It misses the DirName.

What am i doing wrong?

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [November 8, 2021, 1:46pm UTC](https://community.n8n.io/t/function-node-output-data/8988/3 "2021-11-08T13:46:06Z")

</div>

Hi @Mulen, I am not entirely sure I understand what you want to achieve here. In the JS code you have provided, `finalInsert` is a simple String. So you could just write this String into a field of your choice (keeping in mind the n8n [data structure](https://docs.n8n.io/getting-started/key-concepts/#data-structure)). This example workflow runs your code and writes the result in a field of an n8n item:

> **Example Workflow**
>

Looking at your example data it seems, however, you want to mix data from two different sources? I’d try to keep it as simple as possible in this case. Iterate through the array from your first function node and build the string step by step. This example returns both a single string and multiple items, depending on your preference:

> **Example Workflow**
>

 ![image](https://europe1.discourse-cdn.com/n8n/original/2X/c/c6458a7bb1feb7223a7613ffbd0fdd32d048acd4.png) ![image](https://europe1.discourse-cdn.com/n8n/original/2X/c/c02ccbf98ce81a76f6b1b91be4147a7827a996e8.png)

Hope these examples help you get started 🙂

Depending on your exact input format you might also want to check whether your are able to rid yourself of (all or some) JS code through helper nodes such as [Merge](https://docs.n8n.io/nodes/n8n-nodes-base.merge/) or [Item Lists](https://docs.n8n.io/nodes/n8n-nodes-base.itemLists/).

---

<div class="post-metadata">

**Author:** ![Mulen](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@Mulen](https://community.n8n.io/u/Mulen)\
**Post date:** [November 8, 2021, 2:01pm UTC](https://community.n8n.io/t/function-node-output-data/8988/4 "2021-11-08T14:01:49Z")

</div>

@MutedJam

From the example i mentioned the goal is to get the output data just like it appears on Function node

```auto
[
{
"DirName": "mour"
},
{
"DirName": "dmyk"
},
{
"DirName": "oiad"
},
{
"DirName": "deko"
},
{
"DirName": "dvia"
}
] 

```

I thought by adding on the second Function node this

```auto
const values = $items("Function");

```

I would get the above array but it doesn’t work. The values it gets are like this

 ![values](https://europe1.discourse-cdn.com/n8n/original/2X/f/fe1ebe6d5640955da0f6a4ef41df9f258afe80de.png)

I want to get the Array as is from the output of Function 1 node.

```auto
[
{
"DirName": "mour"
},
{
"DirName": "dmyk"
},
{
"DirName": "oiad"
},
{
"DirName": "deko"
},
{
"DirName": "dvia"
}
] 

```

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [November 8, 2021, 2:12pm UTC](https://community.n8n.io/t/function-node-output-data/8988/5 "2021-11-08T14:12:24Z")

</div>

I think I am a bit lost here. So you don’t want to see `(NULL,5,mour,NOW), (NULL,5,dmyk,NOW), (NULL,5,oiad,NOW), (NULL,5,deko,NOW), (NULL,5,dvia,NOW)` as the output, neither as individual items nor as a single string?

Can you provide an example of the result you want to see? Proper n8n items like so?

 ![image](https://europe1.discourse-cdn.com/n8n/original/2X/a/a9e833ab24d2cbffce29dd4b9ae074a0d0324aab.png)

That’d be much simpler:

> **Example Workflow**
>

This also works without JS code (apart from the example data):

> **Example Workflow**
>

---

<div class="post-metadata">

**Author:** ![Mulen](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@Mulen](https://community.n8n.io/u/Mulen)\
**Post date:** [November 8, 2021, 2:25pm UTC](https://community.n8n.io/t/function-node-output-data/8988/6 "2021-11-08T14:25:06Z")

</div>

I’ll try to describe it more clearly.

I have created this script as i said below

```auto
let j=[
{
"DirName": "mour"
},
{
"DirName": "dmyk"
},
{
"DirName": "oiad"
},
{
"DirName": "deko"
},
{
"DirName": "dvia"
}
];

let values=j.reduce((o,a)=>{
                    let ini=[];
                    ini.push("(", "NULL", 5, a.DirName, "NOW", ")");
                    o.push(ini);
                    return o;
              },[]);
              
let forInsert = values.join(', ');
let finalInsert = forInsert.replace(/\(\,/g, "(").replace(/\,\)/g, ")");

console.log(finalInsert);

```

The output is

```auto
(NULL,5,mour,NOW), (NULL,5,dmyk,NOW), (NULL,5,oiad,NOW), (NULL,5,deko,NOW), (NULL,5,dvia,NOW)

```

So i want to do it via n8n.

I create a function node which holds the data

Function1

```auto
let j=[
{
"DirName": "mour"
},
{
"DirName": "dmyk"
},
{
"DirName": "oiad"
},
{
"DirName": "deko"
},
{
"DirName": "dvia"
}
];

return j.map(e => { return { json: e } });

```

Now the json output of that node is

```auto
[
{
"DirName": "mour"
},
{
"DirName": "dmyk"
},
{
"DirName": "oiad"
},
{
"DirName": "deko"
},
{
"DirName": "dvia"
}
] 

```

So far so good.

Now on Function2 node i want to get the Array from Function1 node as is so that i run the script i wrote and the output should be

```auto
[
{
"finalInsert": "(NULL,5,mour,NOW), (NULL,5,dmyk,NOW), (NULL,5,oiad,NOW), (NULL,5,deko,NOW), (NULL,5,dvia,NOW)"
}
] 

```

The thing is on Function2 Node i add

```auto
const mergeValues = $items("Function");

let values=mergeValues.reduce((o,a)=>{
                    let ini=[];
                    ini.push("(", "NULL", 5, a.DirName, "NOW", ")");
                    o.push(ini);
                    return o;
              },[]);

let forInsert = values.join(', ');
let finalInsert = forInsert.replace(/\(\,/g, "(").replace(/\,\)/g, ")");

let insertArray = [{finalInsert}];

return insertArray.map(e => { return { json: e } });

```

But the

```auto
const mergeValues = $items("Function");

```

Does not get the output array from Function1 node as is.

I just want to get the array

```auto
[
{
"DirName": "mour"
},
{
"DirName": "dmyk"
},
{
"DirName": "oiad"
},
{
"DirName": "deko"
},
{
"DirName": "dvia"
}
] 

```

from Function1 node and assign it to a variable in Function2 node

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [November 8, 2021, 2:32pm UTC](https://community.n8n.io/t/function-node-output-data/8988/7 "2021-11-08T14:32:02Z")

</div>

> [@Mulen](#):
>
> But the
> 
> ```auto
> const mergeValues = $items("Function");
> 
> ```
> 
> Does not get the output array from Function1 node as is.

This code inside your **Function1** is getting all output items from your **Function** node though. If you add a `console.log(mergeValues);` in the line thereafter, you can also see the items in your browser console:

 ![image](https://europe1.discourse-cdn.com/n8n/original/2X/b/b30eea31504a8a4911d472b93b9726fd19f6852c.png)

Your subsequent `mergeValues.reduce` does, however, not reference DirName correctly. If you replace `a.DirName` with `a.json.DirName` you should get your result string including the DirName:

 ![image](https://europe1.discourse-cdn.com/n8n/original/2X/d/d6d7b8fa8e20fadd8f666eaf068d959965ec44fb.png)

---

<div class="post-metadata">

**Author:** ![Mulen](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@Mulen](https://community.n8n.io/u/Mulen)\
**Post date:** [November 8, 2021, 2:37pm UTC](https://community.n8n.io/t/function-node-output-data/8988/8 "2021-11-08T14:37:18Z")

</div>

> [@MutedJam](#):
>
> a.json.DirName

Yes thank you. That was what i was missing.

Thank you for your patience and help!
