Array split join basics

Hi. There are many posts regarding split:join arrays through function, however, I am having problems finding what I think should be relatively simple:

  1. Extract a single part from each array object, in this case "msg.payload.state"
  2. Remove all other msg.payload attributes
  3. Reassemble the array objects with only the msg.payload[i] = msg.payload,state[i]

Note, the array is entering NR pre-assembled with each object hosting multiple msg.payload components.

Thanks.

1 Like

Hi,

anything is possible with javascript in a function.
Is it possible to copy/paste a sample of your data to work with ?
5-6 elements of the array of objects will do .. just to have some real data to experiment with

Thanks, @UnborN. I think I figured it out. Added split node from Array then switch node to change msg.payload = msg.payload.state and join. Viola.

[{"id":"b4101700.63b118","type":"debug","z":"bf354502.549c58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1130,"y":1140,"wires":[]},{"id":"538d44ce.3173ec","type":"join","z":"bf354502.549c58","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":910,"y":1140,"wires":[["b4101700.63b118"]]},{"id":"e243b109.cf2a","type":"split","z":"bf354502.549c58","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":570,"y":1140,"wires":[["ef7bfa9.ce70b08"]]},{"id":"ef7bfa9.ce70b08","type":"change","z":"bf354502.549c58","name":"Change State","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.state","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":1140,"wires":[["538d44ce.3173ec"]]}]
1 Like

It seems that you have taken the long path :wink:
Out of curiosity have you tried using only a Change node with Move setting ?
Does this produce the desired result ?

image

1 Like

The use of change node would be great, will it work for all of the objects in the array or do I need to split it first?

S

You dont need to split the array and then join in afterwards
The Move option in Change node should move anything under msg.payload.state to msg.payload
whether thats an array, object, string, number etc

Only one way to find out :wink:
do another wire from your data to the Change node with the Move setting and an extra debug node and test it :wink:

1 Like

When putting change/move, then the msg.payload returns "undefined", split then move then join works fine.

Thanks!

you are right .. i did a quick test with fake data and it is as you say

If you want to change each array object, you would need to use JSONata in a change node for that. But I think that might just add to the confusion here (another thing to learn).

In a function node, you will want to make use of the array function .forEach() - assuming your array is in msg.payload:

msg.payload.forEach( obj => {
    // now `obj` contains each object in the array in turn.
})

A function node containing this should do it

msg.payload = msg.payload.map(x => x.state)
return msg
2 Likes

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