# Simplifying syntax of If..Else. in a function node

**URL:** <https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956>\
**Category:** General\
**Tags:** function-node\
**Created:** [20 September 2022 02:03 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956 "2022-09-20T02:03:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [20 September 2022 02:03 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/1 "2022-09-20T02:03:01Z")

</div>

I want to conditionally set the value of a `flow.context` based on the `msg.payload` value

i am able to achieve the same with following, _(see first three lines)_, but is there an easy way (less code/better code) I have a bunch of them to set.

PS: the value that is checked is actually a string "\_\_null", i have used a slice function before this node

```auto
if (msg.M00==="__null"){
flow.set("M0009"," _____ 0");}else{
flow.set("M0009", msg.M00)};

flow.set("M0109", msg.M01);
flow.set("M0209", msg.M02);
flow.set("M0309", msg.M03);
flow.set("M0409", msg.M04);
flow.set("M0509", msg.M05);
flow.set("M0609", msg.M06);
flow.set("M0709", msg.M07);
flow.set("M0809", msg.M08);
flow.set("M0909", msg.M09);
flow.set("M1009", msg.M10);

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 September 2022 05:08 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/2 "2022-09-20T05:08:51Z")

</div>

Something like this (or at least for some inspiration)

Untested:

```auto
for(let x=0;x<=10;x++){

    let m = x < 10 ? `M0${x}` : 'M10'
    
    if (msg[m] == "__null"){
        flow.set(`${m}09`, ' _____ 0')
    } else {
        flow.set(`${m}09`, msg[m])
    }

}

```

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [20 September 2022 06:37 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/3 "2022-09-20T06:37:37Z")

</div>

Would it not be easier/smarter rather than slicing them to store them as Objects in context store and then just reference each of the values ?

Obviously we would need to see the whole incoming message before you slice it - but i am sure one of the JSONATA gurus on here would be able to turn it into Context variables pretty quickly - particularly if you are going to have a few of them

Craig

---

<div class="post-metadata">

**Author:** ![9toejack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/9toejack/32/56868_2.png) [@9toejack](https://discourse.nodered.org/u/9toejack)\
**Post date:** [20 September 2022 06:57 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/4 "2022-09-20T06:57:38Z")

</div>

so I have different data come in from an analog to digital converter.  
its pressure readings.  
here is function code that I had help with.  
this may get you in the correct direction if they are all coming in one message and you need to split and give each part its own message name.

```auto
let msg0 = {};
let msg1 = {};
let msg2 = {};
let msg3 = {};
//let msg4 = {};

msg0.payload = msg.payload.POWER1;
msg1.payload = msg.payload.POWER2;
msg2.payload = msg.payload.POWER3;
msg3.payload = msg.payload.POWER4;

msg0.topic = "POWER1"
msg1.topic = "POWER2"
msg2.topic = "POWER3"
msg3.topic = "POWER4"

return [msg0, msg1, msg2, msg3];

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [20 September 2022 07:27 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/5 "2022-09-20T07:27:26Z")

</div>

Would need to understand the incoming data to know if there is an easier way.

Here are two ways

```auto
for(let i = 0; i < 11; i++){
    let m = "M" + i.toString().padStart(2, "0");    
    let m9 = m + "09";
    if (msg[m] === "__null"){
        flow.set(m9, " _____ 0");
    }else{
        flow.set(m9, msg[m])
    }
}

```

untested, may be typo's

[edit]  
Or, assuming msg has no other properties

```auto
msg = {M00: "__null", M01: "test", _msgid: "wrttytw555"}; // for testing
let M_set = [];
let M_msg = [];
delete msg._msgid;
for(const[key,value] of Object.entries(msg)){
    M_set.push(key + "09");
    if(value === "__null"){
        M_msg.push(" _____ 0");
    }else{
        M_msg.push(value);
    }
}
flow.set(M_set,M_msg);

```

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [20 September 2022 12:54 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/6 "2022-09-20T12:54:38Z")

</div>

> [@craigcurtin](#):
>
> before you slice it

by slicing i mean to use the function slice to get exact number of characters in the output

```auto
var A00 = (" ______" + msg.payload[0].s0).slice(-6);

```

> [@E1cid](#):
>
> Would need to understand the incoming data to know if there is an easier way.

the incoming data is an array by a MySql query output.

```auto
[{"date":"2022-09-20T00:31:00.000Z","s0":81,"s1":0,"s2":0,"s3":393,"s4":0,"s5":1,"s6":1,"s7":134,"s8":0,"s9":0,"s10":925,"s11":1568,"s12":0,"s13":220,"s14":0,"s15":0,"s16":996,"s17":0}]

```

one such array for each hour, and then i store the values in flow.context for 8 hours and send out as a Shift Report (8 Hour period) for every hour.

the 'issue ' was that since each machine output has a different number of integers the report was not aligned for each hour and difficult to read,

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/b/3b562907f737887991851728a3f428d672a13c71.png)

so i added a slice function to get around that to have a visually cleaner report, that i send through mail. since the report template is fixed for 8 hours, and for the hour which is still not passed, i am getting 'null', to replace that null with a zero, i had used the if else statement, to simplify that code was the intention of my original post.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22b60e2da2de7da1a1248f38f35aa20dc53de3a8.png)

if there are no null values, the report just looks fine

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/7510325336979dd4712eaaa3a7f9fa1132c51d78.png)

> [@E1cid](#):
>
> Here are two ways

will give it a try and report.

Thanks everyone. !

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [20 September 2022 13:26 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/7 "2022-09-20T13:26:45Z")

</div>

If all you want to do is replace "\_\_\_\_\_null" with "\_\_\_\_\_\_\_\_0", would it be simpler just to do a  
`text_output = text_output.replace(/ ___null/g, "______ 0");`  
On the finished report text.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [20 September 2022 22:12 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/8 "2022-09-20T22:12:48Z")

</div>

Perfect!.

_there is always a simpler solution._

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [21 September 2022 02:18 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/9 "2022-09-21T02:18:21Z")

</div>

> [@smanjunath211](#):
>
> so i added a slice function to get around that to have a visually cleaner report, that i send through mail.

is there a better way to align the texts properly ? like a TAB character between words ?

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [21 September 2022 09:45 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/10 "2022-09-21T09:45:49Z")

</div>

I got the solution here. now the table looks much better. no need for slicing to get same number of digits!

> [@Safe tab characters in template nodes?](https://discourse.nodered.org/t/safe-tab-characters-in-template-nodes/49763/2):
>
> what editor are you using? ACE or Monaco? In monaco, it may be changing your tabs to spaces automatically (friendly if thats what you want slight_smile ) You can press f1 and type tabs to ensure it inserts tabs. [vEWeD33LtY]

---

<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:** [5 October 2022 09:46 UTC](https://discourse.nodered.org/t/simplifying-syntax-of-if-else-in-a-function-node/67956/11 "2022-10-05T09:46:21Z")

</div>

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