# Search Array and output Boolean

**URL:** <https://discourse.nodered.org/t/search-array-and-output-boolean/24341>\
**Category:** General\
**Created:** [6 April 2020 17:28 UTC](https://discourse.nodered.org/t/search-array-and-output-boolean/24341 "2020-04-06T17:28:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadamson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nadamson/32/67454_2.png) [@nadamson](https://discourse.nodered.org/u/nadamson)\
**Post date:** [6 April 2020 17:28 UTC](https://discourse.nodered.org/t/search-array-and-output-boolean/24341/1 "2020-04-06T17:28:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [6 April 2020 17:34 UTC](https://discourse.nodered.org/t/search-array-and-output-boolean/24341/2 "2020-04-06T17:34:15Z")

</div>

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:

> **[Array.prototype.includes()](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.

  
Johannes

---

<div class="post-metadata">

**Author:** ![nadamson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nadamson/32/67454_2.png) [@nadamson](https://discourse.nodered.org/u/nadamson)\
**Post date:** [6 April 2020 18:24 UTC](https://discourse.nodered.org/t/search-array-and-output-boolean/24341/3 "2020-04-06T18:24:46Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e8753785b251c275da3f7822ed329a067db7fcbd.png)  
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

```auto
["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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [6 April 2020 18:41 UTC](https://discourse.nodered.org/t/search-array-and-output-boolean/24341/4 "2020-04-06T18:41:35Z")

</div>

very close 👍, try:

```auto
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

---

<div class="post-metadata">

**Author:** ![nadamson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nadamson/32/67454_2.png) [@nadamson](https://discourse.nodered.org/u/nadamson)\
**Post date:** [6 April 2020 18:47 UTC](https://discourse.nodered.org/t/search-array-and-output-boolean/24341/5 "2020-04-06T18:47:43Z")

</div>

Thanks Johannes! That worked!

---

<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:** [20 April 2020 18:58 UTC](https://discourse.nodered.org/t/search-array-and-output-boolean/24341/6 "2020-04-20T18:58:24Z")

</div>

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