How do you use object or index key in a function?

I would like to add the object key for an array to the end of a value in the array.
The reason being the "value" is the same for each array and I need to make it unique.
Maybe there is a better way than using the object key?
My payload always has array of 6 and I would like to add a number of 1 to 6 to each payload.type.

I made this flow using the split node but I have to move my payload around. Is there a better way to do it in a function node?

[{"id":"eef3323764cd3f6e","type":"debug","z":"d03c03b6d2f91f47","name":"debug 57","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1340,"y":1460,"wires":[]},{"id":"fcbfa4b178e6673e","type":"change","z":"d03c03b6d2f91f47","name":"","rules":[{"t":"set","p":"channels","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"flow"},{"t":"set","p":"payload.channels","pt":"msg","to":"channels","tot":"msg"},{"t":"delete","p":"channels","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1160,"y":1460,"wires":[["eef3323764cd3f6e"]]},{"id":"c0f41e1e18652adf","type":"join","z":"d03c03b6d2f91f47","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":990,"y":1460,"wires":[["fcbfa4b178e6673e"]]},{"id":"2788d161010e0c13","type":"function","z":"d03c03b6d2f91f47","name":"function 2","func":"msg.payload.type = msg.payload.type + \"_\"+(msg.parts.index +1)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":1460,"wires":[["c0f41e1e18652adf"]]},{"id":"fbdc32fd3e861c36","type":"split","z":"d03c03b6d2f91f47","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":690,"y":1460,"wires":[["2788d161010e0c13"]]},{"id":"7d2d9127fe8ec2a3","type":"change","z":"d03c03b6d2f91f47","name":"","rules":[{"t":"set","p":"payload","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"payload.channels","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":1460,"wires":[["fbdc32fd3e861c36"]]},{"id":"e88793e63de75c5e","type":"switch","z":"d03c03b6d2f91f47","name":"","property":"payload.channels[0].type","propertyType":"msg","rules":[{"t":"eq","v":"MEASUREMENT_TYPE_PULSE_CNT_ACC_MAJOR","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":1460,"wires":[["7d2d9127fe8ec2a3"],[]]},{"id":"bfa1898edd12117c","type":"inject","z":"d03c03b6d2f91f47","name":"Water Payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"serial_num\":\"KCwCQYls\",\"battery_status\":true,\"measurement_period_base\":5,\"channels\":[{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MAJOR\",\"timestamp\":1677252090,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MINOR\",\"timestamp\":1677252090,\"start_point\":180,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MAJOR\",\"timestamp\":1677252090,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MINOR\",\"timestamp\":1677252090,\"start_point\":2,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MAJOR\",\"timestamp\":1677252090,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MINOR\",\"timestamp\":1677252090,\"start_point\":4,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}],\"next_transmission_at\":1677253219,\"transfer_reason\":4,\"signal\":30,\"measurement_period_factor\":1,\"hash\":6,\"cloud_token\":\"\"}","payloadType":"json","x":160,"y":1460,"wires":[["e88793e63de75c5e"]]}]

possibly like this ..

msg.payload.channels.map( (el, index) => el.type = `${el.type}_${index+1}`)
return msg;

Test Flow

[{"id":"1695480ccfc0767b","type":"inject","z":"a479605cda9a7cfd","name":"Water Payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"serial_num\":\"KCwCQYls\",\"battery_status\":true,\"measurement_period_base\":5,\"channels\":[{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MAJOR\",\"timestamp\":1677252090,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MINOR\",\"timestamp\":1677252090,\"start_point\":180,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MAJOR\",\"timestamp\":1677252090,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MINOR\",\"timestamp\":1677252090,\"start_point\":2,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MAJOR\",\"timestamp\":1677252090,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},{\"type\":\"MEASUREMENT_TYPE_PULSE_CNT_ACC_MINOR\",\"timestamp\":1677252090,\"start_point\":4,\"sample_offsets\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}],\"next_transmission_at\":1677253219,\"transfer_reason\":4,\"signal\":30,\"measurement_period_factor\":1,\"hash\":6,\"cloud_token\":\"\"}","payloadType":"json","x":200,"y":1240,"wires":[["0d61730ba48965e3","7dc6017029898042"]]},{"id":"0d61730ba48965e3","type":"function","z":"a479605cda9a7cfd","name":"map function","func":"msg.payload.channels.map( (el, index) => el.type = `${el.type}_${index+1}`)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":1240,"wires":[["feeba390969f74cf"]]},{"id":"feeba390969f74cf","type":"debug","z":"a479605cda9a7cfd","name":"debug 59","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":600,"y":1240,"wires":[]},{"id":"7dc6017029898042","type":"debug","z":"a479605cda9a7cfd","name":"debug 60","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":360,"y":1300,"wires":[]}]

Thank you, that is very nice!

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