Javascript help to remove NaN from array

This is actually a follow-up related to a previous post, 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:

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:

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

I found at least two objects that were NaN.

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.

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:

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.

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

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 :smiley:

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.

1 Like

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