Find if item exist function node

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.

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.

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)

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

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.

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

{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

You are already above my knowledge base.

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()

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

{
  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;

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

}
1 Like

And here are two more ways

[{"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

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

JSONata

$$.payload.po_line in $$.payload.line_items.line_number 

Can you not check this during the query ?

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.

This works I had set the po_line to flow. because the join node wont combine it.
Appreciate the help :grinning:

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

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

I will like to try this way as well. I appreciate the help :+1:

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