# Consume an array into function to do math

**URL:** <https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374>\
**Category:** General\
**Created:** [16 April 2022 18:08 UTC](https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374 "2022-04-16T18:08:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [16 April 2022 18:08 UTC](https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374/1 "2022-04-16T18:08:07Z")

</div>

This likely seems simple but after hours of different attempts, I am unable to resolve. I have an array of two values that I wish to do math on in a function node but don't know how to set the variables within the function so I can operate on each value. Here is the debug of the array...thank you in advance  
 ![3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/984f3ee9301a96cd45fc8d481bc942e5f9e28204.png)

---

<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:** [16 April 2022 18:11 UTC](https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374/2 "2022-04-16T18:11:33Z")

</div>

msg.payload[0] is 14.44  
and msg.payload[1] is 0  
so  
msg.payload[0] \* msg.payload[1] would multiply 14.44 by 0.  
Or  
let myvar = msg.payload[0] set myvar to 14.44  
There are other methods but you do not mention what calculation you wish to do.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [16 April 2022 18:31 UTC](https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374/3 "2022-04-16T18:31:57Z")

</div>

Thank you...and yes simple multiplication is all I am looking for. However the syntax in your example does not appear to work, and I fully admit that I am a noob. Here is what is in my function node:

```auto
var volts = msg.payload[0];
var amps = msg.payload[1];
var watts = volts * amps;
return watts;

```

And this is the debug message:  
 ![4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7cd655e3741f6bf9025842a036a7364e23a17787.png)

---

<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:** [16 April 2022 18:33 UTC](https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374/4 "2022-04-16T18:33:39Z")

</div>

You always have to return an object.  
So it would be.

```auto
var volts = msg.payload[0];
var amps = msg.payload[1];
var watts = volts * amps;
msg.payload = watts;
return msg;

```

or any msg property you wish  
`msg.watts = watts;` for example.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [16 April 2022 18:39 UTC](https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374/5 "2022-04-16T18:39:05Z")

</div>

Worked perfectly...I truly appreciate your assistance and very quick reply!!

---

<div class="post-metadata">

**Author:** ![JephthahLoader](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@JephthahLoader](https://discourse.nodered.org/u/JephthahLoader)\
**Post date:** [17 April 2022 14:08 UTC](https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374/6 "2022-04-17T14:08:12Z")

</div>

Hey thanks for the super helpful reply. I'm not sure how I missed that thread. I haven't quite mastered the search function on here. I think I'll pass with him this time around. If i want any further guideline we will contact you here [https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374](https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374)[.](https://floridaloteryresults.com/)

---

<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:** [1 May 2022 14:08 UTC](https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374/7 "2022-05-01T14:08:35Z")

</div>

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