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

**URL:** <https://discourse.nodered.org/t/how-do-you-use-object-or-index-key-in-a-function/75692>\
**Category:** General\
**Created:** [24 February 2023 15:49 UTC](https://discourse.nodered.org/t/how-do-you-use-object-or-index-key-in-a-function/75692 "2023-02-24T15:49:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [24 February 2023 15:49 UTC](https://discourse.nodered.org/t/how-do-you-use-object-or-index-key-in-a-function/75692/1 "2023-02-24T15:49:25Z")

</div>

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.

 ![2023-02-24_8-24-13](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/15d545f76ff1f0f7314a5c617cdd7bb2f402a8bd.png)

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?

```auto
[{"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"]]}]

```

 ![2023-02-24_8-48-45](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/0418f7b47a9e0dea39793e6b8289cd49aa63eb44.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [24 February 2023 16:48 UTC](https://discourse.nodered.org/t/how-do-you-use-object-or-index-key-in-a-function/75692/2 "2023-02-24T16:48:17Z")

</div>

> [@JohnnyPicnic](#):
>
> Is there a better way to do it in a function node?

possibly like this ..

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

```

**Test Flow**

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)\
**Post date:** [24 February 2023 16:56 UTC](https://discourse.nodered.org/t/how-do-you-use-object-or-index-key-in-a-function/75692/3 "2023-02-24T16:56:32Z")

</div>

Thank you, that is very nice!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 March 2023 16:57 UTC](https://discourse.nodered.org/t/how-do-you-use-object-or-index-key-in-a-function/75692/4 "2023-03-10T16:57:31Z")

</div>

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