# Shorten Java Script as Loop

**URL:** <https://discourse.nodered.org/t/shorten-java-script-as-loop/37829>\
**Category:** General\
**Created:** [21 December 2020 07:55 UTC](https://discourse.nodered.org/t/shorten-java-script-as-loop/37829 "2020-12-21T07:55:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nilsbaurx](https://avatars.discourse-cdn.com/v4/letter/n/c68b51/32.png) [@nilsbaurx](https://discourse.nodered.org/u/nilsbaurx)\
**Post date:** [21 December 2020 07:55 UTC](https://discourse.nodered.org/t/shorten-java-script-as-loop/37829/1 "2020-12-21T07:55:32Z")

</div>

Hello all,

does anyone have an idea how I can shorten/simplify this function, also with regard to the fact that I have to use more than 9 variables in the future.

var topic=msg.topic;  
//1  
if (topic=="ns=5;s=Arp.Plc.Eclr/Ergebnis1\_Global\_REAL"){  
return [msg,null,null,null,null,null,null,null,null];

}  
//2  
if (topic=="ns=5;s=Arp.Plc.Eclr/Ergebnis2\_Global\_REAL"){  
return[null,msg,null,null,null,null,null,null,null];

}  
//3  
if (topic=="ns=5;s=Arp.Plc.Eclr/Ergebnis3\_Global\_REAL"){  
return[null,null,msg,null,null,null,null,null,null];

}  
//4  
if (topic=="ns=5;s=Arp.Plc.Eclr/Ergebnis4\_Global\_REAL"){  
return[null,null,null,msg,null,null,null,null,null];

}  
//5  
if (topic=="ns=5;s=Arp.Plc.Eclr/Ergebnis1\_Global\_INT"){  
return[null,null,null,null,msg,null,null,null,null];

}  
//6  
if (topic=="ns=5;s=Arp.Plc.Eclr/Ergebnis2\_Global\_INT"){  
return[null,null,null,null,null,msg,null,null,null];

}  
//7  
if (topic=="ns=5;s=Arp.Plc.Eclr/Ergebnis3\_Global\_INT"){  
return[null,null,null,null,null,null,msg,null,null];

}  
//8  
if (topic=="ns=5;s=Arp.Plc.Eclr/Ergebnis4\_Global\_INT"){  
return[null,null,null,null,null,null,null,msg,null];

}  
//9  
if (topic=="ns=5;s=Arp.Plc.Eclr/Ergebnis5\_Global\_INT"){  
return[null,null,null,null,null,null,null,null,msg];

}

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 December 2020 08:14 UTC](https://discourse.nodered.org/t/shorten-java-script-as-loop/37829/2 "2020-12-21T08:14:22Z")

</div>

This is slightly more manageable...

```auto
//add more tags as required
var tags = {
    "ns=5;s=Arp.Plc.Eclr/Ergebnis1_Global_REAL": 0,
    "ns=5;s=Arp.Plc.Eclr/Ergebnis2_Global_REAL": 1,
    "ns=5;s=Arp.Plc.Eclr/Ergebnis3_Global_REAL": 2,
    "ns=5;s=Arp.Plc.Eclr/Ergebnis4_Global_REAL": 3,
    "ns=5;s=Arp.Plc.Eclr/Ergebnis1_Global_INT": 4,
    "ns=5;s=Arp.Plc.Eclr/Ergebnis2_Global_INT": 5,
    "ns=5;s=Arp.Plc.Eclr/Ergebnis3_Global_INT": 6,
    "ns=5;s=Arp.Plc.Eclr/Ergebnis4_Global_INT": 7,
    "ns=5;s=Arp.Plc.Eclr/Ergebnis5_Global_INT": 8,
}
var length = Object.keys(tags).length;
var result = Array(length);
var tagIndex = tags[msg.topic]; //lookup the tag, get index
if(tagIndex==null){
    node.warn("Unknown TAG: " + msg.topic)
    return null; //dont send anything
}
result[tagIndex] = msg;
return result;

```

demo flow...

```auto
[{"id":"fc1dbb7e.94a428","type":"inject","z":"7e0ada2.e792724","name":"ns=5;s=Arp.Plc.Eclr/Ergebnis4_Global_REAL","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis4_Global_REAL","payload":"123","payloadType":"num","x":370,"y":180,"wires":[["aa1842a4.203b3"]]},{"id":"aa1842a4.203b3","type":"function","z":"7e0ada2.e792724","name":"","func":"//add more tags as required\nvar tags = {\n \"ns=5;s=Arp.Plc.Eclr/Ergebnis1_Global_REAL\": 0,\n \"ns=5;s=Arp.Plc.Eclr/Ergebnis2_Global_REAL\": 1,\n \"ns=5;s=Arp.Plc.Eclr/Ergebnis3_Global_REAL\": 2,\n \"ns=5;s=Arp.Plc.Eclr/Ergebnis4_Global_REAL\": 3,\n \"ns=5;s=Arp.Plc.Eclr/Ergebnis1_Global_INT\": 4,\n \"ns=5;s=Arp.Plc.Eclr/Ergebnis2_Global_INT\": 5,\n \"ns=5;s=Arp.Plc.Eclr/Ergebnis3_Global_INT\": 6,\n \"ns=5;s=Arp.Plc.Eclr/Ergebnis4_Global_INT\": 7,\n \"ns=5;s=Arp.Plc.Eclr/Ergebnis5_Global_INT\": 8,\n}\nvar length = Object.keys(tags).length;\nvar result = Array(length);\nvar tagIndex = tags[msg.topic]; //lookup the tag, get index\nif(tagIndex==null){\n node.warn(\"Unknown TAG: \" + msg.topic)\n return null; //dont send anything\n}\nresult[tagIndex] = msg;\nreturn result;","outputs":9,"noerr":0,"initialize":"","finalize":"","x":500,"y":380,"wires":[["72c2ea3.4ccc714"],["53db5dc4.c5f3c4"],["fffb0435.b7d378"],["a0355818.8a7a28"],["384550cf.de01"],["b01e6251.2c2f"],["e28058d5.d3dc38"],["3d2eca3.962bb36"],["1ec48000.0eb1c"]]},{"id":"72c2ea3.4ccc714","type":"debug","z":"7e0ada2.e792724","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":220,"wires":[]},{"id":"b6fbcc37.e8f18","type":"inject","z":"7e0ada2.e792724","name":"ns=5;s=Arp.Plc.Eclr/Ergebnis5_Global_INT","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis5_Global_INT","payload":"456","payloadType":"num","x":360,"y":260,"wires":[["aa1842a4.203b3"]]},{"id":"53db5dc4.c5f3c4","type":"debug","z":"7e0ada2.e792724","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":260,"wires":[]},{"id":"fffb0435.b7d378","type":"debug","z":"7e0ada2.e792724","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":300,"wires":[]},{"id":"a0355818.8a7a28","type":"debug","z":"7e0ada2.e792724","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":340,"wires":[]},{"id":"384550cf.de01","type":"debug","z":"7e0ada2.e792724","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":380,"wires":[]},{"id":"b01e6251.2c2f","type":"debug","z":"7e0ada2.e792724","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":420,"wires":[]},{"id":"e28058d5.d3dc38","type":"debug","z":"7e0ada2.e792724","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":460,"wires":[]},{"id":"3d2eca3.962bb36","type":"debug","z":"7e0ada2.e792724","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":500,"wires":[]},{"id":"1ec48000.0eb1c","type":"debug","z":"7e0ada2.e792724","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":540,"wires":[]},{"id":"30478f9.68c6c7","type":"inject","z":"7e0ada2.e792724","name":"bad_tag_ns=5;s=Arp.Plc.Eclr/blahblah","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"bad_tag_ns=5;s=Arp.Plc.Eclr/blahblah","payload":"234","payloadType":"num","x":350,"y":220,"wires":[["aa1842a4.203b3"]]}]

```

---

<div class="post-metadata">

**Author:** ![nilsbaurx](https://avatars.discourse-cdn.com/v4/letter/n/c68b51/32.png) [@nilsbaurx](https://discourse.nodered.org/u/nilsbaurx)\
**Post date:** [21 December 2020 08:31 UTC](https://discourse.nodered.org/t/shorten-java-script-as-loop/37829/3 "2020-12-21T08:31:59Z")

</div>

Thank you so much and have a great week!

Greetings

nilsbaurx

---

<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:** [4 January 2021 08:32 UTC](https://discourse.nodered.org/t/shorten-java-script-as-loop/37829/4 "2021-01-04T08:32:03Z")

</div>

Closed on 4 January 2021 08:32 UTC

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