# Simplify function node

**URL:** <https://discourse.nodered.org/t/simplify-function-node/52172>\
**Category:** General\
**Created:** [13 October 2021 16:35 UTC](https://discourse.nodered.org/t/simplify-function-node/52172 "2021-10-13T16:35:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [13 October 2021 16:35 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/1 "2021-10-13T16:35:41Z")

</div>

Hi,

I have the following function node that I'm trying to simplify, instead of listing all array items is there a way to make it loop them?

The number of array items may not always be the same.

```auto
msg.payload = {
    "@type": "MessageCard",
    "@context": "https://schema.org/extensions",
    "themeColor": "0078D7",
    "title": "A Record change",
    "summary": "Card \"A Record change\"",
    "sections": [
        {
            "facts": [
                { "name": "Warning", "value": msg.topicstatus },
                { "name": "A record", "value": msg.domain },
                { "name": "Added IP", "value": msg.payload[0] },
                { "name": "Added IP", "value": msg.payload[1] },
                { "name": "Added IP", "value": msg.payload[2] },
                { "name": "Added IP", "value": msg.payload[3] },
                { "name": "Added IP", "value": msg.payload[4] },
                { "name": "Added IP", "value": msg.payload[5] },
                { "name": "Added IP", "value": msg.payload[6] },
                { "name": "Added IP", "value": msg.payload[7] },
                { "name": "Added IP", "value": msg.payload[8] },
                { "name": "Added IP", "value": msg.payload[9] },
                { "name": "Added IP", "value": msg.payload[10] },
                { "name": "Added IP", "value": msg.payload[11] },
                { "name": "Added IP", "value": msg.payload[12] },
                { "name": "Added IP", "value": msg.payload[13] }],
        }
    ]
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [13 October 2021 16:43 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/2 "2021-10-13T16:43:10Z")

</div>

> [@Romprod](#):
>
> I have the following function node that I'm trying to simplify, instead of listing all array items is there a way to make it loop them?

Of course, that is just simple JavaScript. If you aren't familiar, you may wish to quickly run through a simple JavaScript course.

As `msg.payload` is clearly an array, you can loop through arrays in several ways but this is the one I tend to use most:

```auto
msg.payload.forEach( element => {
   // do stuff here, element contains each of the payload elements msg.payload[x]
}

```

Something like this (untested):

```js
const newpayload = {
    "@type": "MessageCard",
    "@context": "https://schema.org/extensions",
    "themeColor": "0078D7",
    "title": "A Record change",
    "summary": "Card \"A Record change\"",
    "sections": [
        {
            "facts": [
                { "name": "Warning", "value": msg.topicstatus },
                { "name": "A record", "value": msg.domain },
            ]
        }
    ]
}

msg.payload.forEach( element => {
    newpayload.sections.facts.push( { "name": "Added IP", "value": element} )
}

return { topic: msg.topic, payload: newpayload }

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 October 2021 09:34 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/4 "2021-10-14T09:34:27Z")

</div>

Note - this was in reply to someone who hijacked the thread and subsequently deleted her posts. It's not relevant to the original poster's enquiry

I cannot make sense of your example msg.payload, the use of single and double quotes looks wrong.

However, at least in MySQL, you can have a statement like this

```auto
INSERT INTO fruits (id, name, price) VALUES (17, "Peach", 4.5)
ON DUPLICATE KEY UPDATE name = "Peach", price = 4.5;

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 October 2021 09:36 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/6 "2021-10-14T09:36:55Z")

</div>

A function node uses javascript, yes.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 October 2021 09:48 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/8 "2021-10-14T09:48:33Z")

</div>

In your first post you said that you wanted to insert new data into a database or update existing records.  
Now you say that you want to display new data on a web page.

I expect it is possible to do both of these things in a single function node. But if I were doing it I would break the task down between nodes appropriate to each step.

Perhaps it would make things clearer if you post your flow as it currently is, including some real data. Use the "preformatted text" option to post your code.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 October 2021 09:53 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/10 "2021-10-14T09:53:13Z")

</div>

> [@Archita](#):
>
> Sure - can I get your email address?

Sorry, no.  
But I (and others) am happy to help in the forum.

---

<div class="post-metadata">

**Author:** ![Archita](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Archita](https://discourse.nodered.org/u/Archita)\
**Post date:** [14 October 2021 09:55 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/11 "2021-10-14T09:55:18Z")

</div>

no worries.  
well the diagram and the query is a prototype for the project so unfortunately, cannot share it. but thanks for your time though. 🙂

---

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [14 October 2021 12:05 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/12 "2021-10-14T12:05:17Z")

</div>

Hi,

Thanks for your example. I had to edit it slightly as there was single bit of code that needed adding above the return line at the bottom.

```auto
msg.payload.forEach( element => {
    newpayload.sections.facts.push( { "name": "Added IP", "value": element} )
    }
)
return { topic: msg.topic, payload: newpayload }

```

When I now run this I get the following error back.

```auto
TypeError: Cannot read property 'push' of undefined

```

My full config

```auto
const newpayload = {
    "@type": "MessageCard",
    "@context": "https://schema.org/extensions",
    "themeColor": "0078D7",
    "title": "A Record change",
    "summary": "Card \"A Record change\"",
    "sections": [
        {
            "facts": [
                { "name": "Warning", "value": msg.topicstatus },
                { "name": "A record", "value": msg.domain },
            ]
        }
    ]
}

msg.payload.forEach( element => {
    newpayload.sections.facts.push( { "name": "Added IP", "value": element} )
    }
)
return { topic: msg.topic, payload: newpayload }

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [14 October 2021 12:48 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/13 "2021-10-14T12:48:58Z")

</div>

> [@Romprod](#):
>
> ```auto
> {
> "facts": [
> { "name": "Warning", "value": msg.topicstatus },
> { "name": "A record", "value": msg.domain },
> { "name": "Added IP", "value": msg.payload[0] },
> { "name": "Added IP", "value": msg.payload[1] },
> { "name": "Added IP", "value": msg.payload[2] },
> { "name": "Added IP", "value": msg.payload[3] },
> ... etc ...
> { "name": "Added IP", "value": msg.payload[13] }],
> }
> 
> ```

You can also use a JSONata expression in a `change` node -- which avoids needing to know how _many_ elements are in the payload array:

```auto
{
    "@type": "MessageCard",
    "@context": "https://schema.org/extensions",
    "themeColor": "0078D7",
    "title": "A Record change",
    "summary": "Card \"A Record change\"",
    "sections": [
        {
            "facts": [
                { "name": "Warning", "value": msg.topicstatus },
                { "name": "A record", "value": msg.domain },
                payload.{ "name": "Added IP", "value": $ }
            ]
        }
    ]
}

```

Just make sure to select the `J:` expression value (not JSON ;\*) -- you can even copy some real data from your debug nodes into the Test expression area, and see how the output will look before deploying your flow, like so:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/239644af4c80aba71cbda6e54dc7858610b9f184.jpeg)

Many people prefer to use plain Javascript code, but I like to use JSONata for restructuring data, like in this use case... so I'm including an example in case you had never tried to use expressions before.

---

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [14 October 2021 13:48 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/14 "2021-10-14T13:48:05Z")

</div>

Thankyou for your detailed explanation and example, I've certainly learnt from that.

That is now working.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 October 2021 13:48 UTC](https://discourse.nodered.org/t/simplify-function-node/52172/15 "2021-10-28T13:48:22Z")

</div>

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