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.
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.
             
            
              
            
           
          
            
              
                JGKK  
                
                  
                    6 April 2020 17:34
                   
                  2 
               
             
            
              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:
             
            
              
            
           
          
            
            
              Thank you for the reply.
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.
             
            
              
            
           
          
            
              
                JGKK  
              
                  
                    6 April 2020 18:41
                   
                  4 
               
             
            
              very close 
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.
             
            
              
            
           
          
            
            
              Thanks Johannes! That worked!
             
            
              1 Like 
            
            
           
          
            
              
                system  
              
                  
                    20 April 2020 18:58
                   
                  6 
               
             
            
              This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.