# Check msg.payload in array

**URL:** <https://discourse.nodered.org/t/check-msg-payload-in-array/69756>\
**Category:** General\
**Tags:** function-node\
**Created:** [29 October 2022 04:22 UTC](https://discourse.nodered.org/t/check-msg-payload-in-array/69756 "2022-10-29T04:22:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yingkhun](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yingkhun/32/39651_2.png) [@yingkhun](https://discourse.nodered.org/u/yingkhun)\
**Post date:** [29 October 2022 04:22 UTC](https://discourse.nodered.org/t/check-msg-payload-in-array/69756/1 "2022-10-29T04:22:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [29 October 2022 05:00 UTC](https://discourse.nodered.org/t/check-msg-payload-in-array/69756/2 "2022-10-29T05:00:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 October 2022 06:33 UTC](https://discourse.nodered.org/t/check-msg-payload-in-array/69756/3 "2022-10-29T06:33:46Z")

</div>

You could use [includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#try_it) method

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

---

<div class="post-metadata">

**Author:** ![yingkhun](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yingkhun/32/39651_2.png) [@yingkhun](https://discourse.nodered.org/u/yingkhun)\
**Post date:** [29 October 2022 06:46 UTC](https://discourse.nodered.org/t/check-msg-payload-in-array/69756/4 "2022-10-29T06:46:31Z")

</div>

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

---

<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:** [12 November 2022 06:47 UTC](https://discourse.nodered.org/t/check-msg-payload-in-array/69756/5 "2022-11-12T06:47:11Z")

</div>

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