# Combining values from an Array into a single word

**URL:** <https://discourse.nodered.org/t/combining-values-from-an-array-into-a-single-word/90003>\
**Category:** General\
**Created:** [5 August 2024 14:18 UTC](https://discourse.nodered.org/t/combining-values-from-an-array-into-a-single-word/90003 "2024-08-05T14:18:33Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 August 2024 19:39 UTC](https://discourse.nodered.org/t/combining-values-from-an-array-into-a-single-word/90003/21 "2024-08-05T19:39:27Z")

</div>

> [@haplc2000](#):
>
> Also, the code is giving error while importing

What error? Please be specific.

What version of node-red are you using? How did you import (step-by-step)

---

<div class="post-metadata">

**Author:** ![haplc2000](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@haplc2000](https://discourse.nodered.org/u/haplc2000)\
**Post date:** [5 August 2024 20:06 UTC](https://discourse.nodered.org/t/combining-values-from-an-array-into-a-single-word/90003/22 "2024-08-05T20:06:26Z")

</div>

OK everyone  
I will try to work form here on own. No need to troll - in fact no time troll as this is already 10 PM here.

Thanks anyway

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 August 2024 20:07 UTC](https://discourse.nodered.org/t/combining-values-from-an-array-into-a-single-word/90003/23 "2024-08-05T20:07:15Z")

</div>

The payload you pasted above has a property "value" which is an array of 8 javascript objects.

```auto
msg.payload.value[0].value.value = 219
msg.payload.value[1].value.value = 299
msg.payload.value[2].value.value = {"0":-1,"1":-1,"2":-1,"3":-1}
msg.payload.value[3].value.value = 12336
msg.payload.value[4].value.value = false
msg.payload.value[5].value.value = false
msg.payload.value[6].value.value = true
msg.payload.value[7].value.value = {"0":0,"1":0,"2":0,"3":5}

```

Note that Node-red sees `msg.payload.value[2 and 7].value.value` as objects not arrays.

So you want to replace `msg.payload.value[2].value.value` with "-1-1-1-1"?  
And `msg.payload.value[2].value.value` with "0005" ?  
Is it always value[2] and value[7]?

Here is a flow to do the above, but I feel sure it can't be what you really need. I am not familiar with Modbus and it's data structure.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/efb6a337fcda1c1d52447a7f9fe38b5ca3e941f4.png)

```auto
[{"id":"03010988047a67e4","type":"inject","z":"dc102a70652e7fb1","name":"Sample data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"payload\":1722599750410,\"payloadType\":\"read\",\"value\":[{\"value\":{\"dataType\":\"Int16\",\"arrayType\":\"Scalar\",\"value\":291},\"statusCode\":{\"value\":0},\"sourceTimestamp\":\"2024-08-02T11:55:51.528Z\",\"sourcePicoseconds\":424000000,\"serverTimestamp\":\"2024-08-02T11:55:51.528Z\",\"serverPicoseconds\":420000000},{\"value\":{\"dataType\":\"Int16\",\"arrayType\":\"Scalar\",\"value\":299},\"statusCode\":{\"value\":0},\"sourceTimestamp\":\"2024-08-02T11:55:51.552Z\",\"sourcePicoseconds\":403000000,\"serverTimestamp\":\"2024-08-02T11:55:51.552Z\",\"serverPicoseconds\":399000000},{\"value\":{\"dataType\":\"Int16\",\"arrayType\":\"Array\",\"value\":{\"0\":-1,\"1\":-1,\"2\":-1,\"3\":-1}},\"statusCode\":{\"value\":0},\"sourceTimestamp\":\"2024-08-02T11:55:52.104Z\",\"sourcePicoseconds\":80000000,\"serverTimestamp\":\"2024-08-02T11:55:52.104Z\",\"serverPicoseconds\":76000000},{\"value\":{\"dataType\":\"Int16\",\"arrayType\":\"Scalar\",\"value\":12336},\"statusCode\":{\"value\":0},\"sourceTimestamp\":\"2024-08-02T11:55:52.127Z\",\"sourcePicoseconds\":857000000,\"serverTimestamp\":\"2024-08-02T11:55:52.127Z\",\"serverPicoseconds\":852000000},{\"value\":{\"dataType\":\"Boolean\",\"arrayType\":\"Scalar\",\"value\":false},\"statusCode\":{\"value\":0},\"sourceTimestamp\":\"2024-08-02T11:55:52.132Z\",\"sourcePicoseconds\":646000000,\"serverTimestamp\":\"2024-08-02T11:55:52.132Z\",\"serverPicoseconds\":643000000},{\"value\":{\"dataType\":\"Boolean\",\"arrayType\":\"Scalar\",\"value\":false},\"statusCode\":{\"value\":0},\"sourceTimestamp\":\"2024-08-02T11:55:52.136Z\",\"sourcePicoseconds\":758000000,\"serverTimestamp\":\"2024-08-02T11:55:52.136Z\",\"serverPicoseconds\":755000000},{\"value\":{\"dataType\":\"Boolean\",\"arrayType\":\"Scalar\",\"value\":true},\"statusCode\":{\"value\":0},\"sourceTimestamp\":\"2024-08-02T11:55:52.141Z\",\"sourcePicoseconds\":229000000,\"serverTimestamp\":\"2024-08-02T11:55:52.141Z\",\"serverPicoseconds\":225000000},{\"value\":{\"dataType\":\"Int16\",\"arrayType\":\"Array\",\"value\":{\"0\":0,\"1\":0,\"2\":0,\"3\":5}},\"statusCode\":{\"value\":0},\"sourceTimestamp\":\"2024-08-02T11:55:52.685Z\",\"sourcePicoseconds\":159000000,\"serverTimestamp\":\"2024-08-02T11:55:52.685Z\",\"serverPicoseconds\":156000000}],\"nodetype\":\"read\",\"injectType\":\"read\",\"addressSpaceItems\":[{\"name\":\"TempChan2\",\"nodeId\":\"ns=0;i=50343\",\"datatypeName\":\"\"},{\"nodeId\":\"ns=0;i=50349\"},{\"nodeId\":\"ns=0;i=50601\"},{\"nodeId\":\"ns=0;i=50640\"},{\"nodeId\":\"ns=0;i=50722\"},{\"nodeId\":\"ns=0;i=50723\"},{\"nodeId\":\"ns=0;i=50724\"},{\"nodeId\":\"ns=0;i=50547\"}],\"manualInject\":true,\"readtype\":\"VariableValue\",\"attributeId\":13,\"justValue\":true}","payloadType":"json","x":150,"y":60,"wires":[["4e1b29fbaf4f77a4","18849ef309e63841"]]},{"id":"4e1b29fbaf4f77a4","type":"function","z":"dc102a70652e7fb1","name":"Combine data in [2] and [7]","func":"function combine (object) {\n let combined = \"\"\n for (let key in object) {\n combined += object[key]\n }\n return combined\n}\nmsg.payload.value[2].value.value = combine(msg.payload.value[2].value.value)\nmsg.payload.value[7].value.value = combine(msg.payload.value[7].value.value)\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":120,"wires":[["e3e9d77778a278a4"]]},{"id":"e3e9d77778a278a4","type":"debug","z":"dc102a70652e7fb1","name":"Output data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":120,"wires":[]},{"id":"18849ef309e63841","type":"debug","z":"dc102a70652e7fb1","name":"Input data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":60,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![haplc2000](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@haplc2000](https://discourse.nodered.org/u/haplc2000)\
**Post date:** [5 August 2024 20:15 UTC](https://discourse.nodered.org/t/combining-values-from-an-array-into-a-single-word/90003/24 "2024-08-05T20:15:00Z")

</div>

Thanks a lot jbudd.

Very kind and understanding.

I think, I was not able to state the problem clearly.  
Look at this:  
msg.payload.value[7].value.value = {"0":0,"1":0,"2":0,"3":5}  
This means msg.payload.value[7].value.valueis an array with 4 objects.

What I need to do get value from msg.payload.value[7].value.value /object 1 which is 0,  
= {"0":0,"1":0,"2":0,"3":5}  
get value from msg.payload.value[7].value.value /object 2 which is 0,  
get value from msg.payload.value[7].value.value /object 3 which is 0,  
get value from msg.payload.value[7].value.value /object 5 which is 5,

then put these values 0005 in a variable (integer)

Another point I would like to mention:  
to read object 1 of msg.payload.value[7].value.value, I am using: payload.value[7].value.value[0]

to read object 2 of msg.payload.value[7].value.value, I am using: payload.value[7].value.value[1]

to read object 3 of msg.payload.value[7].value.value, I am using: payload.value[7].value.value[2]

to read object 4 of msg.payload.value[7].value.value, I am using: payload.value[7].value.value[3]  
Thanks again

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 August 2024 20:32 UTC](https://discourse.nodered.org/t/combining-values-from-an-array-into-a-single-word/90003/25 "2024-08-05T20:32:22Z")

</div>

> [@haplc2000](#):
>
> msg.payload.value[7].value.value = {"0":0,"1":0,"2":0,"3":5}  
> This means msg.payload.value[7].value.value is an array with 4 objects.

To be pedantic that is an object with 4 key/value properties.  
If it was an array debug would show it as [0,0,0,5]

I do not know whether or not this particular object can be regarded as an array (since the keys are "0" to "3") but it's probably best to treat it as an object, whatever it says in `msg.payload.value[7].value.arrayType`!

> [@haplc2000](#):
>
> Another point I would like to mention:  
> to read object 1 of msg.payload.value[7].value.value, I am using: payload.value[7].value.value[0]

Yes that does suggest that this particular object can be treated as an array.

> [@haplc2000](#):
>
> then put these values 0005 in a variable (integer)

Sorry but 0005 is not an integer, it's a string. 5 is an integer.

I cannot offer any more help. I hope my offerings above lead you to the solution.

---

<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:** [3 November 2024 20:33 UTC](https://discourse.nodered.org/t/combining-values-from-an-array-into-a-single-word/90003/26 "2024-11-03T20:33:09Z")

</div>

Closed on 3 November 2024 20:33 UTC

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

[Previous page](https://discourse.nodered.org/t/combining-values-from-an-array-into-a-single-word/90003.md?page=1)
