# How do I set an array to a variable

**URL:** https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997
**Category:** General
**Created:** [8 June 2020 15:02 UTC](https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997 "2020-06-08T15:02:34Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![gohyuheng02](https://avatars.discourse-cdn.com/v4/letter/g/c77e96/32.png) [@gohyuheng02](https://discourse.nodered.org/u/gohyuheng02)
#### Post date: [8 June 2020 15:02 UTC](https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997/1 "2020-06-08T15:02:34Z")

</div>

For example in the given array; (array is updated with different value every sec)  
[93,30,9]

I would like to indicate that 93 is humidity, 30 is temperature, 9 is x in JSON format. [which look something like this ( "humidity": 93, "temperature": 30, "x": 9 )]

Any idea what the code is like in the function node?  
var humidity;  
msg.payload = msg.payload.humidity;  
return msg;

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [8 June 2020 15:13 UTC](https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997/2 "2020-06-08T15:13:37Z")

</div>

Assuming the array if passed in the msg.

```auto
msg.payload = {"humidity": msg[0], 
               "temperature": msg[1], 
               "x": msg[2]
};
return msg;

```

---

<div class="post-metadata">

### Author: ![gohyuheng02](https://avatars.discourse-cdn.com/v4/letter/g/c77e96/32.png) [@gohyuheng02](https://discourse.nodered.org/u/gohyuheng02)
#### Post date: [9 June 2020 02:43 UTC](https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997/3 "2020-06-09T02:43:02Z")

</div>

![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/8764d19113ceae53a3540a234aa68900067f6df6.jpeg)

This error occurred when I input your function code

---

<div class="post-metadata">

### Author: ![gohyuheng02](https://avatars.discourse-cdn.com/v4/letter/g/c77e96/32.png) [@gohyuheng02](https://discourse.nodered.org/u/gohyuheng02)
#### Post date: [9 June 2020 02:44 UTC](https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997/4 "2020-06-09T02:44:09Z")

</div>

alright got it working thanks !!

msg.payload = {"humidity": msg.payload[0],  
"temperature": msg.payload[1],  
"vibration": msg.payload[2]  
};  
return msg;

---

<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: [9 June 2020 08:01 UTC](https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997/5 "2020-06-09T08:01:16Z")

</div>

For future reference, since they do not contain any special characters, you do not need to put the attribute names humidity, temperature or vibration in quotes.

---

<div class="post-metadata">

### Author: ![gohyuheng02](https://avatars.discourse-cdn.com/v4/letter/g/c77e96/32.png) [@gohyuheng02](https://discourse.nodered.org/u/gohyuheng02)
#### Post date: [9 June 2020 08:43 UTC](https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997/6 "2020-06-09T08:43:55Z")

</div>

does that mean if i remove the quote, it will still work normally?

---

<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: [9 June 2020 09:25 UTC](https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997/7 "2020-06-09T09:25:36Z")

</div>

Yes  
`x = {name: 1}`  
is identical to  
`x = {"name": 1}`  
If, however the attribute had unusual characters, a minus sign for example, or a space, or starts with a number, then you would need the quotes  
`x = {"name-1": 1}`  
as otherwise it will try to interpret the - as a minus sign.  
If you were creating a JSON string though (as opposed to a javascript object) then you would need  
`string = '{"name":1}'`  
in order to build a valid JSON string.

---

<div class="post-metadata">

### Author: ![gohyuheng02](https://avatars.discourse-cdn.com/v4/letter/g/c77e96/32.png) [@gohyuheng02](https://discourse.nodered.org/u/gohyuheng02)
#### Post date: [10 June 2020 03:15 UTC](https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997/8 "2020-06-10T03:15:42Z")

</div>

alright cool thanks for the info!

---

<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: [24 June 2020 03:15 UTC](https://discourse.nodered.org/t/how-do-i-set-an-array-to-a-variable/27997/9 "2020-06-24T03:15:49Z")

</div>

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