Node Red 1.3.4v substring

I am new to Node Red I want to display data from serial port on Dashboard.
The in coming data example #!1234567890123456 split into 4 substrings
Substring1 = 123
Substring2 = 456
Substring3 = 78901
Substring4 = 23456

One way wouldbe to use a function node and Slice() String.prototype.slice() - JavaScript | MDN
example flow

[{"id":"f6332b85.359ec8","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#!1234567890123456","payloadType":"str","x":280,"y":120,"wires":[["4c3a662e.52be7"]]},{"id":"4c3a662e.52be7","type":"function","z":"bf9e1e33.030598","name":"","func":"const string = msg.payload;\nconst bits = [[\"name1\",2,5],[\"name2\",5,8],[\"name3\",8,13],[\"name4\",13,19]];\nmsg.payload = bits.reduce((acc, arr) => {\n    return Object.assign(acc, {[arr[0]]:string.slice(arr[1],arr[2])});\n},{});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":60,"wires":[["9efd2c72.cb6e88"]]},{"id":"9efd2c72.cb6e88","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":100,"wires":[]}]

Output

{
    "name1": "123",
    "name2": "456",
    "name3": "78901",
    "name4": "23456"
}

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