# Selecting specific value in an array using var

**URL:** <https://discourse.nodered.org/t/selecting-specific-value-in-an-array-using-var/25641>\
**Category:** General\
**Created:** [27 April 2020 19:17 UTC](https://discourse.nodered.org/t/selecting-specific-value-in-an-array-using-var/25641 "2020-04-27T19:17:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![opwierde](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/opwierde/32/21051_2.png) [@opwierde](https://discourse.nodered.org/u/opwierde)\
**Post date:** [27 April 2020 19:17 UTC](https://discourse.nodered.org/t/selecting-specific-value-in-an-array-using-var/25641/1 "2020-04-27T19:17:54Z")

</div>

A bit of a beginners question but I am new to JS and Node-RED.

I am reading values directly from my solar inverter web interface and am struggling with the kWh value. I have the data from the inverter in an array that lists the values in the order of days of the month. This is what I put in a function block (global.today contains the day of the month):

 ![Screen Shot 2020-04-27 at 9.11.54 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8ce7fdd71fd1b92b42778d7fde4e72f39a10c1a3.png)

I want to select the correct value using var today: payload: msg.payload[today]  
That does not work. If I test by sticking a number in, [27] for example, it does work.  
Is there a solution for this?

---

<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:** [27 April 2020 19:35 UTC](https://discourse.nodered.org/t/selecting-specific-value-in-an-array-using-var/25641/2 "2020-04-27T19:35:25Z")

</div>

Impossible to tell unless you show us what is in global.today (my guess is it's empty or not between 0 & 29) also, expand the debug payload so we can see what's in it.

> [@opwierde](#):
>
> If I test by sticking a number in, [27] for example, it does work.  
> Is there a solution for this

Like I said above

Try putting node.warn("today = " + today) below `var today` then look in the debug sidebar - tell us what value you get

Regarding arrays...  
you should always check that the index (`today`) is actually a number and that is \>= 0 and \< the array length (`msg.payload.length`) BEFORE trying to access an element of an array.

Ps, look at the side bar "context" to see what is in global.today.

---

<div class="post-metadata">

**Author:** ![opwierde](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/opwierde/32/21051_2.png) [@opwierde](https://discourse.nodered.org/u/opwierde)\
**Post date:** [27 April 2020 19:49 UTC](https://discourse.nodered.org/t/selecting-specific-value-in-an-array-using-var/25641/3 "2020-04-27T19:49:24Z")

</div>

Thanks for the quick reply!

 ![Screen Shot 2020-04-27 at 9.41.01 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30d12bf5d9850f4ac99ce51d4a7d3e97eebf5a3d.png)

 ![Screen Shot 2020-04-27 at 9.43.12 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/43b14240bdb5e4b72195b686cdbd040936ac13d8.png)

"today" looks like a number in the context screen, but not in the node.warn line

The array:  
`["9.638","8.927","11.47","19.44","19.22","18.93","19.67","19.11","18.53","19.92","19.93","18.86","16.56","17.19","21.32","20.01","22.43","22.25","22.49","22.61","22.62","22.33","21.89","22.17","21.91","18.94","22.49","0.000","0.000","0.000"]`

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 April 2020 19:53 UTC](https://discourse.nodered.org/t/selecting-specific-value-in-an-array-using-var/25641/4 "2020-04-27T19:53:36Z")

</div>

To access `today` you need to use `global.get("today")` not `global.today`

---

<div class="post-metadata">

**Author:** ![opwierde](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/opwierde/32/21051_2.png) [@opwierde](https://discourse.nodered.org/u/opwierde)\
**Post date:** [27 April 2020 20:04 UTC](https://discourse.nodered.org/t/selecting-specific-value-in-an-array-using-var/25641/5 "2020-04-27T20:04:46Z")

</div>

Thanks! Works like a charm.

 ![Screen Shot 2020-04-27 at 10.02.50 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc9e3e633acc645ca1eedf1cae59362512b14309.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:** [26 June 2020 20:11 UTC](https://discourse.nodered.org/t/selecting-specific-value-in-an-array-using-var/25641/6 "2020-06-26T20:11:50Z")

</div>

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