# Search an Array

**URL:** <https://discourse.nodered.org/t/search-an-array/81969>\
**Category:** General\
**Created:** [12 October 2023 12:04 UTC](https://discourse.nodered.org/t/search-an-array/81969 "2023-10-12T12:04:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elad23](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/elad23/32/83275_2.png) [@Elad23](https://discourse.nodered.org/u/Elad23)\
**Post date:** [12 October 2023 12:04 UTC](https://discourse.nodered.org/t/search-an-array/81969/1 "2023-10-12T12:04:27Z")

</div>

Hi,

I have a text file that I am loading into an array which contains lots of license plates.

I have a feed coming from a camera with msg.payload of licene plate data; I want to be able to search every message coming out of this to see if it exists in the license plate array and if it does then continue...

Any ideas?

Thanks

D

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 October 2023 12:08 UTC](https://discourse.nodered.org/t/search-an-array/81969/2 "2023-10-12T12:08:47Z")

</div>

Welcome to the forums @Elad23

For anyone to be able to provide solutions, can you provide an example payload that you receive (of course with dummy plates 😬)

Also can we assume your array is flat?

`["xxxxxxxx", "xxxxxxxx", "xxxxxxxx"]`

---

<div class="post-metadata">

**Author:** ![Elad23](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/elad23/32/83275_2.png) [@Elad23](https://discourse.nodered.org/u/Elad23)\
**Post date:** [12 October 2023 12:20 UTC](https://discourse.nodered.org/t/search-an-array/81969/3 "2023-10-12T12:20:38Z")

</div>

Of course.. here is what I load from the CSV into a set of objects:

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

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 October 2023 12:28 UTC](https://discourse.nodered.org/t/search-an-array/81969/4 "2023-10-12T12:28:02Z")

</div>

Ok its still a little unclear on your setup, but I'll give it ago.

1. Store your object array in a global variable (`KnownPlates`)
2. When the camera detects a plate - check it

I'm going to pretend I know where the plate data is in the camera provided payload.

`msg.payload.detectedLicensePlate` 🤷‍♂️

```js
const plates = global.get("KnownPlates")
const found = plates.find((p) => p.plate === msg.payload.detectedLicensePlate)
if(found !== undefined){
    // continue (use found.plate/name/apartment etc etc)
}

```

Use that in a `function` node

I'm sure this can be achieved with a `switch` node and `JSONata` also.

**EDIT**  
I updated the sample, so you can use The found plate

---

<div class="post-metadata">

**Author:** ![Elad23](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/elad23/32/83275_2.png) [@Elad23](https://discourse.nodered.org/u/Elad23)\
**Post date:** [12 October 2023 13:47 UTC](https://discourse.nodered.org/t/search-an-array/81969/5 "2023-10-12T13:47:48Z")

</div>

Thanks for the input Marcus...

I am using the CSV node with output single message array - how do I make this into a global that can then be read by the function that is reading the license plate data?

(As you have probably gathered I am a little new to node.red...)

---

<div class="post-metadata">

**Author:** ![Elad23](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/elad23/32/83275_2.png) [@Elad23](https://discourse.nodered.org/u/Elad23)\
**Post date:** [12 October 2023 13:53 UTC](https://discourse.nodered.org/t/search-an-array/81969/6 "2023-10-12T13:53:04Z")

</div>

Forget that - I read the forum and worked that out 🙂

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 October 2023 13:53 UTC](https://discourse.nodered.org/t/search-an-array/81969/7 "2023-10-12T13:53:53Z")

</div>

You can do this by triggering the `inject` Node after 1s of Node RED starting.  
and use the `change` node to set the global.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/681d1947c45da0fb5e6dcbcb71cb14f4ca49c266.png)

Then my code example _should_ work when attached to the camera output

---

<div class="post-metadata">

**Author:** ![Elad23](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/elad23/32/83275_2.png) [@Elad23](https://discourse.nodered.org/u/Elad23)\
**Post date:** [12 October 2023 13:55 UTC](https://discourse.nodered.org/t/search-an-array/81969/8 "2023-10-12T13:55:34Z")

</div>

I did it this way (which I think also works):  
global.set("KnownPlates",msg.payload);

---

<div class="post-metadata">

**Author:** ![Elad23](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/elad23/32/83275_2.png) [@Elad23](https://discourse.nodered.org/u/Elad23)\
**Post date:** [12 October 2023 15:02 UTC](https://discourse.nodered.org/t/search-an-array/81969/9 "2023-10-12T15:02:44Z")

</div>

OK so that seems all ok... now how do I return a different value in that object?

e.g. I have ammended the CSV so now it also has "parking" in it, which denotes which garage door they have access to... this is either a 1, 2 or 3 value and I want to return that numerical value so I can put into a switch to control the three relays 🙂

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 October 2023 15:36 UTC](https://discourse.nodered.org/t/search-an-array/81969/10 "2023-10-12T15:36:34Z")

</div>

Hi @Elad23

With my original code, `found` becomes the object, with that in mind....

you prob should rename `found` to `driver`..... DONE

```auto
const plates = global.get("KnownPlates")
const driver = plates.find((p) => p.plate === msg.payload.detectedLicensePlate)
if(driver !== undefined){
    msg.payload = {
       relayId: driver.parking,
       driverName: driver.name
    }
    return msg
}

```

This will then output a `payload` message of

```auto
{
  relayId: 5,
  driverName: 'Bob Geldof'
}

```

---

<div class="post-metadata">

**Author:** ![Elad23](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/elad23/32/83275_2.png) [@Elad23](https://discourse.nodered.org/u/Elad23)\
**Post date:** [12 October 2023 15:45 UTC](https://discourse.nodered.org/t/search-an-array/81969/11 "2023-10-12T15:45:46Z")

</div>

Then in the switch I put after that how do I get the value of the specific relayID in the switch - or do I need to seperate the array first?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [12 October 2023 16:35 UTC](https://discourse.nodered.org/t/search-an-array/81969/12 "2023-10-12T16:35:12Z")

</div>

Hi @Elad23

The code is already extracting 1 element out of the array (based on the plate number match)  
and setting a new `payload` with properties from the matched object

```js
if(driver !== undefined){
    /* HERE */
    msg.payload = {
       relayId: driver.parking,
       driverName: driver.name
    }
    return msg
}

```

So in a `switch` node further down the track, you can do...

 ![Screenshot 2023-10-12 at 17.34.17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6d4a649554e32fc53278c6f8d4677e8e32ec3ec.png)

You can set the `payload` to the entire matched object, given you all the properties, but you need to use `payload.parking` in the `switch` Node - or rename the CSV header for it 😉

```js
if(driver !== undefined){
    msg.payload = driver
    return msg
}

```

---

<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:** [11 December 2023 16:35 UTC](https://discourse.nodered.org/t/search-an-array/81969/13 "2023-12-11T16:35:56Z")

</div>

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