# How to check if payload is a subset of an array in function nodes

**URL:** <https://discourse.nodered.org/t/how-to-check-if-payload-is-a-subset-of-an-array-in-function-nodes/48893>\
**Category:** General\
**Created:** [25 July 2021 20:35 UTC](https://discourse.nodered.org/t/how-to-check-if-payload-is-a-subset-of-an-array-in-function-nodes/48893 "2021-07-25T20:35:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [25 July 2021 20:35 UTC](https://discourse.nodered.org/t/how-to-check-if-payload-is-a-subset-of-an-array-in-function-nodes/48893/1 "2021-07-25T20:35:37Z")

</div>

Hi there,

following situation: I integrated the "Open weather map" node which delivers current weather information for my location. This information is encoded using different IDs for different weather conditions (i.e. 202 - thunderstorm with heavy rain, 221 - ragged thunderstorm etc.). I´m using a change node to extract this information from the original object and set a corresponding msg.payload.

Now comes the challenge: I would like to use a function node to check whether the weather ID is subset of a defined array of IDs (true) or not (false). I´m sure this is easy Java Script magic but all attempts that I trief failed miserably 😬 Some Pros around that can help me with that? 🙂

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [25 July 2021 20:43 UTC](https://discourse.nodered.org/t/how-to-check-if-payload-is-a-subset-of-an-array-in-function-nodes/48893/2 "2021-07-25T20:43:05Z")

</div>

A javascript switch statement?  
[https://www.w3schools.com/js/js\_switch.asp](https://www.w3schools.com/js/js_switch.asp)

---

<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:** [25 July 2021 20:45 UTC](https://discourse.nodered.org/t/how-to-check-if-payload-is-a-subset-of-an-array-in-function-nodes/48893/3 "2021-07-25T20:45:50Z")

</div>

Or maybe?

> **[Array.prototype.includes() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)**
>
> The includes() method determines whether an array
> includes a certain value among its entries, returning true or
> false as appropriate.

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [25 July 2021 20:45 UTC](https://discourse.nodered.org/t/how-to-check-if-payload-is-a-subset-of-an-array-in-function-nodes/48893/4 "2021-07-25T20:45:50Z")

</div>

Thanks for the quick answere @Paul-Reed. That probably is an option, but I would need to define a case for every weather condition. Is there not a more easy way where I can define an array of numbers and check the payload against that?

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [25 July 2021 20:56 UTC](https://discourse.nodered.org/t/how-to-check-if-payload-is-a-subset-of-an-array-in-function-nodes/48893/5 "2021-07-25T20:56:30Z")

</div>

Thanks @E1cid. I tried sth. like this now

```auto
const CurrentState = msg.payload;
const WeatherIDs = ['202', '212'];

console.log(WeatherIDs.includes(CurrentState));

```

Is that correct and if so how do I pass on the result?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [25 July 2021 21:08 UTC](https://discourse.nodered.org/t/how-to-check-if-payload-is-a-subset-of-an-array-in-function-nodes/48893/6 "2021-07-25T21:08:10Z")

</div>

Try;

```auto
const CurrentState = msg.payload;
const WeatherIDs = ['202', '212'];
msg.payload = (WeatherIDs.includes(CurrentState));
return msg;

```

Note: the incoming msg.payload would be a string `"202"` and not a number `202` as your example is expecting `const WeatherIDs = ['202', '212'];`

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [25 July 2021 21:11 UTC](https://discourse.nodered.org/t/how-to-check-if-payload-is-a-subset-of-an-array-in-function-nodes/48893/7 "2021-07-25T21:11:03Z")

</div>

Perfect, that´s it @Paul-Reed and also thanks for the notice of the strings! 🙂

---

<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:** [8 August 2021 21:11 UTC](https://discourse.nodered.org/t/how-to-check-if-payload-is-a-subset-of-an-array-in-function-nodes/48893/8 "2021-08-08T21:11:45Z")

</div>

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