Search Array and output Boolean

Hello All,

I have an array that I would like to be able to search for certain strings.

Here is an example of the array output in the Debug window.
["LA","NJ","NJ-NJ","NJ-NJ","NJ-NJ","NJ-NJ","MS1","WA"]

Then I would like to search for "NJ"

If NJ is found, then the output should say True or 1, else it should say 0 or false.

You can use the javascript array.includes() in a function node to do this. This method return true if a value is in the array. Have a look here:


Johannes

Thank you for the reply.
Sorry I'm new to this and still learning.
So here is my setup.


I have the API dumping out a json file and have it parsed into msg.payload.
Next I have the Switch configured to look at only the states which gets me the array output of

["LA","NJ","NJ-NJ","NJ-NJ","NJ-NJ","NJ-NJ","MS1","WA"]

and that is dumping out to msg.cities.

In the function node, I have:

msg.cities.includes("WA");
return msg;

I am showing a WA in the msg.cities debug. But the function node is still outputting false, or in a normal debug block it shows Undefined.

very close :+1:, try:

const bool = msg.cities.includes("WA");
msg.payload = bool;
return msg;

the include method doesn’t change the array but is a function that returns a boolean value. so you need to assign that returned value to a var.
Johannes

Thanks Johannes! That worked!

1 Like

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