Shorten Java Script as Loop

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];

}

This is slightly more manageable...

//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...

[{"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"]]}]

Thank you so much and have a great week!

Greetings

nilsbaurx

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