Hey Node-Red forum,
I'm new here even thou i have been using node-red for some time now, i have never come across a specific issue that didn't have a clear answer to in this forum. But now I'm facing an small issue I have yet to figure out or find any solution for.
So the msg.payload i send is an Array of objects that each object includes a number of an old code. This code I want to translate and re-send as a string in a new Array Object payload with the updated match. And preferable only the update code number.
But at some points I got a reply that includes several types of replies and at some cases I get nothing in the returned answer from the regex expression.
What am I doing wrong here?
[{"id":"69e480a0e348fc09","type":"tab","label":"first","disabled":false,"info":"","env":[]},{"id":"b0899b0f5fceb2cd","type":"inject","z":"69e480a0e348fc09","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":210,"y":340,"wires":[["268049f9463a8378"]]},{"id":"94fcc599bfcf579e","type":"debug","z":"69e480a0e348fc09","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":340,"wires":[]},{"id":"13cbc46f94182716","type":"debug","z":"69e480a0e348fc09","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":400,"wires":[]},{"id":"268049f9463a8378","type":"function","z":"69e480a0e348fc09","name":"","func":"var msg1 ={ oldCode: 5 }\nvar msg2 ={ oldCode: 10 } \nvar msg3 ={ oldCode: 45 } \nvar msg4 ={ oldCode: 46 } \nvar msg5 ={ oldCode: 44 } \nvar msg6 ={ oldCode: 24 } \nvar msg7 ={ oldCode: 23 } \nvar msg8 ={ oldCode: 22 } \nvar msg9 ={ oldCode: 16 } \nvar msg10 ={ oldCode: 12 }\nvar msg11 ={ oldCode: 0 }\n\nmsg.payload = new Array ([msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11])\n \nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":340,"wires":[["13cbc46f94182716","6d023ca9588e23a7"]]},{"id":"6d023ca9588e23a7","type":"function","z":"69e480a0e348fc09","name":"","func":"var codes = [\n {\n oldCode: '5',\n newCode: '09090909-0909'\n },\n {\n oldCode: '0',\n newCode: '09020200-0000'\n },\n {\n oldCode: '10',\n newCode: '02010100-0000'\n },\n {\n oldCode: '12',\n newCode: '06010200-0000'\n },\n {\n oldCode: '16',\n newCode: '06010500-3312'\n },\n {\n oldCode: '22',\n newCode: '05010100-0000'\n },\n {\n oldCode: '23',\n newCode: '05010200-0000'\n },\n {\n oldCode: '24',\n newCode: '05020101-3312'\n },\n {\n oldCode: '44',\n newCode: '05024100-3312'\n },\n {\n oldCode: '45',\n newCode: '05024200-3312'\n },\n {\n oldCode: '46',\n newCode: '05024300-3312'\n }\n \n];\n\nvar result = JSON.stringify(msg.payload);\ncodes.forEach(e => {\n var numberPattern = /(\\\\d)+(\\\\.)/g\n var oldones = e.oldCode.match(numberPattern);\n var searchRegExp = new RegExp(e.oldCode, ''); \n result = result.replace(searchRegExp, e.newCode);\n})\nvar newCode = result.split(\",\",)\nmsg.payload = ([newCode])\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":340,"wires":[["94fcc599bfcf579e"]]}]