# How can I get array components values

**URL:** <https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820>\
**Category:** General\
**Created:** [12 November 2018 22:01 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820 "2018-11-12T22:01:44Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [12 November 2018 22:01 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/1 "2018-11-12T22:01:44Z")

</div>

Hi Guy, I´m a beginner on node-red and I tried get a values from a sample array of number. I tried to use function with a lot of methods inside.  
my array have the follow format.

![array%20format](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e8d9aba087786f2b7c4ab5b978773183213b497f.jpeg)

I need to separate each component of array to do a different data processing.

somebody can help me?

Best regards,  
Alex

1. List item

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 November 2018 07:49 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/2 "2018-11-13T07:49:30Z")

</div>

Note that this is not an Array it is a Buffer, which is not the same thing. However, if you just want individual bytes as numbers then you can access as if it is an array of 8 bit integers. So you can use, for example  
`var x = msg.payload[2]`  
to get element 2. There are a number of questions though. Is that what you are trying to do or do you want the buffer split into a sequence of messages each containing one value or what?

If you want one number per message then first convert it to an explicit array using

```auto
msg.payload = [...msg.payload]
return msg

```

then feed it into a Split node and it will give you a sequence of messages, each with one number.

---

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [16 November 2018 01:46 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/3 "2018-11-16T01:46:57Z")

</div>

Hi Colin, thanks a lot for your support.  
to complement your questions, I would like to handling all those elements received in the buffer. I need to parser each one element to trigger a new actions.  
As you advise me , I build an array using msg.payload[n] as you can see bellow:  
msg.payload = msg.payload[0],msg.payload[1],msg.payload[2],msg.payload[3]]  
return msg

And get sucess, but my buffer according specific msg, have a variable elements, for example:  
Cmd1 response:  
 ![cmd1_setch](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b005bf4f56ad3410e5ff12e7a85358f14a9f45d8.jpeg)

**Cmd response 2:**

**Cmd response 3:** and etc....

To improve my parser I need to analyse a number of array received and length for each cmd response for each message and finally analyze each array element.

Do you can help me with examples to implement intelligence in my parser?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 November 2018 09:03 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/4 "2018-11-16T09:03:25Z")

</div>

I am not certain exactly what you want to do, but I think you will have to use a Function node for that and write some javascript. You can get the length of the array using  
`msg.payload.length`  
and you can, for example, loop through the array with something like

```auto
var i;
for (i = 0; i < msg.payload.length; i++) {
    // do something with msg.payload[i]
}

```

Does that help?

---

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [16 November 2018 20:42 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/5 "2018-11-16T20:42:02Z")

</div>

Hi Colin, thanks a lot!  
I made this code to test and working well:  
var i;  
for (i = 0; i \< msg.payload.length; i++) {  
var a;  
a= msg.payload[i]  
if (a==1){  
return msg; //(used to see if works)

```
}

```

}  
right now I need to send a message, via serial port, according for each parameter received using an inject node.

I have a few injection with all those command, I need to building a flow, for example:  
if (a ==2) \> Call Inject 1.  
How can I call a injection in the code to send send a command inside each one object inject?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 November 2018 21:53 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/6 "2018-11-16T21:53:50Z")

</div>

> [@aargollo](#):
>
> How can I call a injection in the code to send send a command inside each one object inject?

If you call `node.send(msg)` inside a function then it will send that message and then carry on. So you can loop through your array working out what to do for each one and sending the appropriate messages on the way through. Then you feed the output of the function node into whatever nodes you need to do what you want.

---

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [17 November 2018 16:10 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/7 "2018-11-17T16:10:49Z")

</div>

Hi Colin, thanks a lot. I had progress in my parser after you help me!  
As you see bellow, right now I can handling the parameters responses and send a command using node.send().

//variables  
var i;

for (i = 0; i \< msg.payload.length; i++) {

// handling parameters and send the response message  
switch (msg.payload[i]){  
//Analyze GetVersion msg and sending the Cmd\_SetCh11  
case 149:{  
//Send Cmd\_SetCh11  
msg.payload = new Buffer([0x01,0x02,0x10,0x21,0x02,0x10,0x02,0x14,0x2D,0x02,0x10,0x02,0x10,0x02,0x18,0x02,0x10,0x03]);  
node.send(msg);  
}  
break;

}  
break;  
}

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 November 2018 16:36 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/8 "2018-11-17T16:36:23Z")

</div>

I can see a slight possible problem with that. The first time it finds msg.payload[i] is 149 then you are overwriting msg.payload with a new buffer, so the next time round the loop it will use the new msg.payload. That will be ok provided you never want to use the original msg.payload again. Even if you don't think you will be ever need the old msg.payload again it might be safer to use

```auto
var newMsg = {payload: new Buffer(..)}
node.send(newMsg)

```

then it will not affect the msg passed in.  
Also I am a bit confused by the last `break`, that will cause it to break out of the for loop after just going through once, with i=0. I suspect that is not what you want.  
Finally do you actually need a Buffer rather than an Array? Arrays are generally easier to handle unless you have a specific need for a Buffer.

For the future, if you are posting code if you put a line containing three backtick characters before it and another after it then that will stop the forum reformatting it and messing up the indentation and so on.

---

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [18 November 2018 01:47 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/9 "2018-11-18T01:47:19Z")

</div>

Hi Mr Colin,

I using buffer to send cmd, via serial, to my exchange device and array to handling responses received and make a specific decision.  
I have two parts in my code. I building a msg flow according msg received, detect a specific byte and than need to send another msg.  
Right now is working well. Thanks a lot!!!!

I need to building a html page, with button inside, to catch in specific function to send msgs. Do you have an link to suggest me to start to study it?

I changed my code as you advise me. As you can see bellow:

//variables  
var i;

// tratamento das respostas aos comandos do gateway  
for (i = 0; i \< msg.payload.length; i++) {

// handling parameters and send a response mesage  
switch (msg.payload[i]){  
//Analyze GetVersion msg and sending the Cmd\_SetCh11  
case 149:{  
//Send Cmd\_SetCh11  
newMsg = {payload: new Buffer([0x01,0x02,0x10,0x21,0x02,0x10,0x02,0x14,0x2D,0x02,0x10,0x02,0x10,0x02,0x18,0x02,0x10,0x03])}  
node.send(newMsg);  
}  
break;  
//Analyze Cmd\_SetCh11 msg and sending the Cmd\_SetDev-Coord  
case 164:{  
//Send Cmd\_SetDev-Coord  
newMsg = {payload: new Buffer([0x01,0x02,0x10,0x23,0x02,0x10,0x02,0x11,0x22,0x02,0x10,0x03])}  
node.send(newMsg);  
}  
break;

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [18 November 2018 07:09 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/10 "2018-11-18T07:09:52Z")

</div>

please see this post which explains how you  
should be posting a flow in the forum

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 November 2018 09:19 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/11 "2018-11-18T09:19:00Z")

</div>

> [@aargollo](#):
>
> I need to building a html page, with button inside, to catch in specific function to send msgs. Do you have an link to suggest me to start to study it?

Probably the standard node red dashboard will do what you need.

> **[node-red-dashboard](https://flows.nodered.org/node/node-red-dashboard)**
>
> A set of dashboard nodes for Node-RED

---

<div class="post-metadata">

**Author:** ![aargollo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@aargollo](https://discourse.nodered.org/u/aargollo)\
**Post date:** [18 November 2018 10:31 UTC](https://discourse.nodered.org/t/how-can-i-get-array-components-values/4820/12 "2018-11-18T10:31:36Z")

</div>

Hi Mr Colin. thanks a lot for your assistance. You are the best!!!  
Mr Ukmose thanks for your advise about how to post a code here!!!

I´ll closing this post, my difficult was overcome.

to contribute to the richness of information, I include below a PIC with information of my code.

Best Regards  
Alex

 ![final%20parsing](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/154441b419a210f85a6a5ded2e9754fe32c7c4db.jpeg)
