Remove end of array in payload

How do I remove 22 thru 27 of the data array as shown? I just want to pass out the first 21 data values.

To make the function 16 modbus write work i need to have the data array be the same as the quantity of values it writes. Im sure its an easy code line so i didnt bother putting the flow or values up properly but i can if anyone needs them.

i would think it would be something like msg.payload.value[21] = msg.payload.data[27]; but this isn't right

You probably want to look at JS's .slice() function.

Looks like that would do it.

Trying
msg.value = data(slice(0,21));

but doesn't work. Im sure just using the syntax wrong

This isn't pretty but works

msg.value = msg.payload.data.slice(0, 21)

Array.prototype.slice()

1 Like

by the way .. i notice in your screenshot that you do a bunch of delete to clean up a msg.
if its easier .. you could do the opposite and create a new msg with a new structure based on some of the values of the old msg.

let newMsg = {
"payload" : "new payload",
"values" : msg.payload.data.slice(0,21),
"topic" : "new topic"
}

return newMsg;

1 Like

Thank you! That would be better. Drinking from the fire hose but getting it working

you are welcome but the solution was given by TotallyInformation :wink:
i just clarified a small part of how to use it correctly.

Best to use the Mozilla Developer Network to look up JS functions - I still have to do that all the time.

Also, deleting things from the msg can sometimes have odd effects due to the way that JavaScript passes variables by reference and not as a copy.

1 Like

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