Check msg.payload in array

hi

I'm new in nodered and nodeJS
I need to know how to check if message in array like python

var my_array = ["a", "b", "c", "d", "e"]
msg.payload = "c"
if msg.payload in my_array {do something}

how to coding

Thank you

If you are new to node-red, I would suggest to learn this "the node-red way", feed the payload (containing the array) into a split node (it splits the array into separate messages) and then use a switch node after that to "route" the message a certain direction. The switch node acts like an "if".

This scenario depends on what you actually want to achieve.

You could use includes() method

let my_array = ["a", "b", "c", "d", "e"]
msg.payload = "c"
if (my_array.includes(msg.payload){
  do something.......
}
1 Like

wow.. that work perfectly thank you now I have learned new thing :wink:

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