Compare to any item in an array

Hello,
I'd like to compare a payload to any item in a certain array. The array will vary in length at any given time, and it's just looking for one match on any item to pass. Sorry, this isn't really Node-Red super specific; more a coding question in general....but you guys rock! I feel like there's a .forEach() in there somewhere, but my lack of experience so far doesn't give me much leash for reasonable experimenting, it turns out.

Hello,
if you are looking for an exact match you can use:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

otherwise you can use:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

which gives you more flexibility in choosing your matching condition like using a regex but would be more complex

Johannes

1 Like

You can use a jsonata expression in a node-red change or switch node that is returning the matching element or returns if the element is found or not in the array.

If you share an example of the array, the payload and the matching criteria then you will definitely find someone who provides you with the answer.

Really, it's almost as simple as it could be. I'll just be comparing a payload of, say 5, to an array of [3, 15, 17, 20, 54], and in this case give a false.
The .includes() works great, but I would like to see what the jsonata would look like for this scenario. I couldn't find anything on a quick peruse of it's array functions in particular.

payload in array

1 Like

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