# Javascript help to remove NaN from array

**URL:** <https://discourse.nodered.org/t/javascript-help-to-remove-nan-from-array/67001>\
**Category:** General\
**Created:** [27 August 2022 10:42 UTC](https://discourse.nodered.org/t/javascript-help-to-remove-nan-from-array/67001 "2022-08-27T10:42:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [27 August 2022 10:42 UTC](https://discourse.nodered.org/t/javascript-help-to-remove-nan-from-array/67001/1 "2022-08-27T10:42:28Z")

</div>

This is actually a follow-up related to a [previous post](https://discourse.nodered.org/t/create-array-from-objects-whilst-using-names-from-current-objects/66719), but is a totally different question...

In an HTTP GET request, I grab the price of nickel (which is returned as a value that must be divided by 1), convert to lbs and then store in Influx, all via this function:

```auto
msg.payload = msg.payload.map(function (value) {
    return {
        measurement: "CommodityData",
        fields: {
            Ni_price: 1.0 /value.NI / 2204.623,
        },
        timestamp: new Date(value.date).valueOf()
    };
});
return msg;

```

I have found that for reasons unknown, when grabbing historical data, it can return NaN (maybe it's a holiday or whatever). Influx does not like this, and displays:

```auto
"Error: invalid float value for field 'Ni_price': NaN"

```

I found at least two objects that were NaN.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/513efac119d01b13b63bc10feef02119b43ed620.png)

Is there a quick and easy way to modify my function above to exclude any time Ni\_price is NaN? Thanks in advance for any pointers.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 August 2022 10:45 UTC](https://discourse.nodered.org/t/javascript-help-to-remove-nan-from-array/67001/2 "2022-08-27T10:45:55Z")

</div>

Ok, I'll try.

Pass the message through a `function` node and look at the message.

Then - using your example - you would have something like:

```auto
if (msg.payload.CommodityData.Ni_price == nan)
{
      // Code to stop the error
}

```

Alas you may need to do a few iterations of the code but I hope that helps.

---

<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 August 2022 11:00 UTC](https://discourse.nodered.org/t/javascript-help-to-remove-nan-from-array/67001/3 "2022-08-27T11:00:18Z")

</div>

> [@Trying\_to\_learn](#):
>
> `== nan`

`nan` is not a thing. The safest way is to use `isNaN`

```auto
if (isNaN(msg.payload.CommodityData.Ni_price)) {
    return null // stop the msg being passed to next node
}
return msg;

```

  

PS, if you use monaco, you will see that 😃

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/a/fab280fd688fa6705e950bea4fe8ad6750fc59f6.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 August 2022 11:01 UTC](https://discourse.nodered.org/t/javascript-help-to-remove-nan-from-array/67001/4 "2022-08-27T11:01:50Z")

</div>

Yes, ok. I didn't actually write the code in NR. I did it on the fly and nope that the mistake would be read around.

It was an example. Sorry, but I am having a bit of trouble at my end and just dropped the quick reply.

---

<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:** [10 September 2022 11:02 UTC](https://discourse.nodered.org/t/javascript-help-to-remove-nan-from-array/67001/5 "2022-09-10T11:02:45Z")

</div>

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