# Working with arrays

**URL:** https://discourse.nodered.org/t/working-with-arrays/73487
**Category:** General
**Tags:** function-node
**Created:** [10 January 2023 17:30 UTC](https://discourse.nodered.org/t/working-with-arrays/73487 "2023-01-10T17:30:49Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![IngoKnito](https://avatars.discourse-cdn.com/v4/letter/i/41988e/32.png) [@IngoKnito](https://discourse.nodered.org/u/IngoKnito)
#### Post date: [10 January 2023 17:30 UTC](https://discourse.nodered.org/t/working-with-arrays/73487/1 "2023-01-10T17:30:49Z")

</div>

Possible Noob-question 🙂 - yes i have seached in the docu and have found not an answer.

I create a msg with:

```auto
var num = msg.payload;
msg.payload = [{ "Test": parseFloat(num) }];
return msg;

```

Now i want to replace the "test" with a variable.

```auto
var num = msg.payload;
var testindex = "Test"
msg.payload = [{ testindex: parseFloat(num) }];
return msg;

```

But this does not work.  
It results in an array with "testindex: 1673371589863" - but not with the variable like "Test: 1673371589863" as expected.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [10 January 2023 17:50 UTC](https://discourse.nodered.org/t/working-with-arrays/73487/2 "2023-01-10T17:50:07Z")

</div>

Is there a reason why it needs to output an array with a single object ?

a dirty way:

```auto
const num = msg.payload;
const property = "test"
msg.payload = [{ [`${property}`]: parseFloat(num) }];
return msg;

```

---

<div class="post-metadata">

### Author: ![IngoKnito](https://avatars.discourse-cdn.com/v4/letter/i/41988e/32.png) [@IngoKnito](https://discourse.nodered.org/u/IngoKnito)
#### Post date: [10 January 2023 18:06 UTC](https://discourse.nodered.org/t/working-with-arrays/73487/3 "2023-01-10T18:06:17Z")

</div>

Ahhh. Great! That works well.  
Thanks for the quick reply! 🙂

To the question: It is for the elimination of multiple function nodes to only one and to avoid typos 😉

---

<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: [10 January 2023 18:27 UTC](https://discourse.nodered.org/t/working-with-arrays/73487/4 "2023-01-10T18:27:43Z")

</div>

> [@bakman2](#):
>
> msg.payload = [{ [` ${property}`]: parseFloat(num) }];

Just for info. The string template is not needed

```auto
msg.payload = [{ [property]: parseFloat(num) }];

```

Should be fine.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [10 January 2023 18:30 UTC](https://discourse.nodered.org/t/working-with-arrays/73487/5 "2023-01-10T18:30:58Z")

</div>

> [@E1cid](#):
>
> Just for info. The string template is not needed

Yeah thats what i thought, it didnt work here (initially), must have made a typo.

---

<div class="post-metadata">

### Author: ![IngoKnito](https://avatars.discourse-cdn.com/v4/letter/i/41988e/32.png) [@IngoKnito](https://discourse.nodered.org/u/IngoKnito)
#### Post date: [10 January 2023 18:56 UTC](https://discourse.nodered.org/t/working-with-arrays/73487/6 "2023-01-10T18:56:06Z")

</div>

Looks good, but does not work.  
The result is "undefined: 1673376899329"

---

<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: [10 January 2023 19:05 UTC](https://discourse.nodered.org/t/working-with-arrays/73487/7 "2023-01-10T19:05:25Z")

</div>

You have some sort of typo or a undefined variable `property`.  
As working perfectly fine here

 ![chrome_screenshot_1673377417634](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a6b01649324bbf11825220907ce49a36346efd68.png)

---

<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: [11 March 2023 19:05 UTC](https://discourse.nodered.org/t/working-with-arrays/73487/8 "2023-03-11T19:05:51Z")

</div>

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