# Find if item exist function node

**URL:** <https://discourse.nodered.org/t/find-if-item-exist-function-node/55502>\
**Category:** General\
**Tags:** function-node\
**Created:** [21 December 2021 02:52 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502 "2021-12-21T02:52:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ecombs](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@ecombs](https://discourse.nodered.org/u/ecombs)\
**Post date:** [21 December 2021 02:52 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/1 "2021-12-21T02:52:25Z")

</div>

I have been trying to figure out if a line number is in a database or not.  
I have created a merged object and i need to check to see if the po\_line is detected in the line\_items array. I have been trying to get it to return a true or false, but I cant seem to get the proper code for the desired output.

```auto
object
po_line: 4
line_items: array[4]
0: object
line_number: 1
1: object
line_number: 2
2: object
line_number: 3
3: object
line_number: 4

```

Any help would be greatly appreciated. I can't seem to find an example on this either.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 December 2021 03:24 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/2 "2021-12-21T03:24:19Z")

</div>

As shown I am not understanding the structure of `po_line`.

Is that different to `line_items`?

If so you should post them as two different things as looking at that they are one.

(Moving on)

You will need to get the size of `line_items` (in this case `4`) and _loop_ that many times checking each against `po_line`.

In basic (sorry but that's the best I can do just now)

```auto
for x = 0 to line_items[]
if (line_items{x} = po_line)
{
   goto found
}
next

echo "Not found"
exit
found
echo "Match found at position " x

```

---

<div class="post-metadata">

**Author:** ![ecombs](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@ecombs](https://discourse.nodered.org/u/ecombs)\
**Post date:** [21 December 2021 04:10 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/3 "2021-12-21T04:10:06Z")

</div>

po\_line is the documents that is uploaded. To make sure I don't have duplicate line in the database, it needs to test to see if it pulls the matching line item under the po number that has been uploaded. The only problem is some orders have 1 line some have 4.

I was going to try and use this function.

```auto
var item_to_add = msg.payload.po_line;

var array1 = msg.payload.line_items;

var output = array1.includes(item_to_add);

msg.payload = output;

return msg;

```

The issue is it always returns false because its dealing with a json current array  
` {line_items:1, line_items:2, line_items:3, line_items:4}` it returns like this because of the output from the mysql node rather than` [1,2,3,4]`.  
Join node wont create an array like

```auto
{po_line:1
line_items:[1,2,3,4]

```

I have only been able to get it like the first one.

If I could get it like this  
the function code should work

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 December 2021 04:11 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/4 "2021-12-21T04:11:42Z")

</div>

You are already above my knowledge base.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [21 December 2021 04:32 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/5 "2021-12-21T04:32:32Z")

</div>

Try putting a `change` node after your join node, select `payload.line_items` as the output field, and use this JSONata `J:` expression to flatten the array of line\_item objects into an array of line numbers:

```
payload.line_items.line_number[]

```

Alternatively, you can use a slightly different J: expression to return a true or false, depending upon whether the line number you are seeking (e.g. 3) is in the array of line\_items already:

```
payload.line_items[line_number = 3]~>$exists()
```

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [21 December 2021 19:24 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/6 "2021-12-21T19:24:45Z")

</div>

If you are just looking to see if the content of `po_line` is in one of the array objects;

```auto
{
  po_line: 4,
  line_items: [
              {line_number: 1},
              {line_number: 2}, 
              {line_number: 3},
              {line_number: 4},
            ]
}

```

if the above Object is in `msg.payload` then you can test for `po_line` like so;

```auto
let po_line = msg.payload.po_line
let line_items = msg.payload.line_items

let found = false
for (let i = 0; i < line.items.length; i++) {
   if (line_items[i].line_number === po_line) {
      found = true
      break

}

```

---

<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:** [21 December 2021 20:00 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/7 "2021-12-21T20:00:03Z")

</div>

And here are two more ways

```auto
[{"id":"8034f1d4.e672","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"po_line\":4,\"line_items\":[{\"line_number\":1},{\"line_number\":2},{\"line_number\":3},{\"line_number\":4}]}","payloadType":"json","x":130,"y":1340,"wires":[["539c5327.bfaebc","4f83af10.44de78"]]},{"id":"539c5327.bfaebc","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.po_line in $$.payload.line_items.line_number ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":1340,"wires":[["1f9c2371.428565"]]},{"id":"4f83af10.44de78","type":"function","z":"c791cbc0.84f648","name":"","func":"msg.payload = msg.payload.line_items.reduce((a,e) => (e.line_number === msg.payload.po_line) || a, false)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":1400,"wires":[["1f9c2371.428565"]]},{"id":"1f9c2371.428565","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":1440,"wires":[]}]

```

Javascript

```auto
msg.payload = msg.payload.line_items.reduce((a,e) => (e.line_number === msg.payload.po_line) || a, false)
return msg;

```

JSONata

```auto
$$.payload.po_line in $$.payload.line_items.line_number 

```

---

<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:** [22 December 2021 05:14 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/8 "2021-12-22T05:14:16Z")

</div>

> [@ecombs](#):
>
> I have been trying to figure out if a line number is in a database or not.

Can you not check this during the query ?

---

<div class="post-metadata">

**Author:** ![ecombs](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@ecombs](https://discourse.nodered.org/u/ecombs)\
**Post date:** [22 December 2021 15:55 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/10 "2021-12-22T15:55:55Z")

</div>

I can check if its empty. However, if there's more than one line number a empty check does me no good. but I see what you mean. I could do that however i will have a long drawn out flow. There can be over 10 line items. I need to be able to see if the existing line number exist in the database.

---

<div class="post-metadata">

**Author:** ![ecombs](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@ecombs](https://discourse.nodered.org/u/ecombs)\
**Post date:** [22 December 2021 15:57 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/11 "2021-12-22T15:57:48Z")

</div>

This works I had set the po\_line to flow. because the join node wont combine it.  
Appreciate the help 😀

side note (if you scroll across the top 5 emoji it makes an animation).

---

<div class="post-metadata">

**Author:** ![ecombs](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@ecombs](https://discourse.nodered.org/u/ecombs)\
**Post date:** [22 December 2021 15:58 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/12 "2021-12-22T15:58:37Z")

</div>

I would like to try this way as well. I appreciate the help

---

<div class="post-metadata">

**Author:** ![ecombs](https://avatars.discourse-cdn.com/v4/letter/e/43a26b/32.png) [@ecombs](https://discourse.nodered.org/u/ecombs)\
**Post date:** [22 December 2021 16:03 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/13 "2021-12-22T16:03:28Z")

</div>

I will like to try this way as well. I appreciate the help 👍

---

<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 January 2022 16:03 UTC](https://discourse.nodered.org/t/find-if-item-exist-function-node/55502/14 "2022-01-05T16:03:39Z")

</div>

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