# Check if value is inside array

**URL:** <https://discourse.nodered.org/t/check-if-value-is-inside-array/87347>\
**Category:** General\
**Created:** [19 April 2024 15:54 UTC](https://discourse.nodered.org/t/check-if-value-is-inside-array/87347 "2024-04-19T15:54:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gravino](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Gravino](https://discourse.nodered.org/u/Gravino)\
**Post date:** [19 April 2024 15:54 UTC](https://discourse.nodered.org/t/check-if-value-is-inside-array/87347/1 "2024-04-19T15:54:40Z")

</div>

hello every one

i would like to create a function to chek if a value is inside array i d this but is not working

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/934bec5fec99a9015607ffccdc882ad326d0bac3.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 April 2024 16:41 UTC](https://discourse.nodered.org/t/check-if-value-is-inside-array/87347/2 "2024-04-19T16:41:52Z")

</div>

Can you copy the data using the "Copy Value" button

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/2/725bc8c5b032e9d1c791b7e4264f4a4bbbae49b1.png)

and paste the msg data into a reply (inside a code block).  
It is hard to recreate data from a screenshot.

  

Also, please dont post pictures of code (they are not searchable and most people wont re-type your code to help you solve it)

---

<div class="post-metadata">

**Author:** ![Gravino](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Gravino](https://discourse.nodered.org/u/Gravino)\
**Post date:** [19 April 2024 17:11 UTC](https://discourse.nodered.org/t/check-if-value-is-inside-array/87347/3 "2024-04-19T17:11:15Z")

</div>

Thank you Steve .... i did thisfor now, i'm really new on node-red but looking in the forum i had lot of help

`{"":"14h00-22h00"}`

```var
var Pippo ;
input = msg.payload;
 var vroeg = input.includes("06h00-14h00",0);
var late = input.includes("14h00-22h00",0);
if(vroeg === true) {
    Pippo = 1;
}

if (late === true){
    Pippo = 2;
} 
    

msg.payload = Pippo;

return msg;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 April 2024 17:13 UTC](https://discourse.nodered.org/t/check-if-value-is-inside-array/87347/4 "2024-04-19T17:13:10Z")

</div>

> [@Gravino](#):
>
> {"":"14h00-22h00"}

where is this data being generated? It is most odd!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 April 2024 17:17 UTC](https://discourse.nodered.org/t/check-if-value-is-inside-array/87347/5 "2024-04-19T17:17:57Z")

</div>

Anyhow, you can use the [`Array.find`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) function to search an array of objects.

e.g:

```auto
let pippo
const input = msg.payload
const vroeg = !!input.find(e => e[''] === '06h00-14h00')
const late = !!input.find(e => e[''] === '14h00-22h00')

if (vroeg === true) {
    pippo = 1
}

if (late === true) {
    pippo = 2
}

msg.payload = pippo

return msg

```

---

<div class="post-metadata">

**Author:** ![Gravino](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Gravino](https://discourse.nodered.org/u/Gravino)\
**Post date:** [19 April 2024 17:22 UTC](https://discourse.nodered.org/t/check-if-value-is-inside-array/87347/6 "2024-04-19T17:22:58Z")

</div>

the data are generated in a sheet of excel i reading a woorkbook if i found this data i will send to a Siemens plc to start warmup of a gloe machine

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 April 2024 17:21 UTC](https://discourse.nodered.org/t/check-if-value-is-inside-array/87347/7 "2024-04-20T17:21:57Z")

</div>

The data object is not really very valid as an object. That's why your includes doesn't work. You are looking in the array for a string but the array contents are objects with a very odd key `""`.

I would suggest fixing the Excel data output.

---

<div class="post-metadata">

**Author:** ![Gravino](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Gravino](https://discourse.nodered.org/u/Gravino)\
**Post date:** [21 April 2024 14:09 UTC](https://discourse.nodered.org/t/check-if-value-is-inside-array/87347/8 "2024-04-21T14:09:59Z")

</div>

thank you very much for suggestion, i just used the function from Steve and is working

thank you very much again

---

<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:** [5 May 2024 14:10 UTC](https://discourse.nodered.org/t/check-if-value-is-inside-array/87347/9 "2024-05-05T14:10:23Z")

</div>

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