Search inside an object of arrays

Hi , i am having problem searching a specific string inside an object of arrays, i have tried the node of viseo for searching an object with the result "Unfound Object". Also i have tried with two for loops searching the whole object and i constantly got "Can not read property 0 of undefined", then i searched the forum and i found 2 solution the one with json command match which got me an answer "undefined", and onw using wildcards with the same answer as before.!image
this is my object located at msg.dbase and the string i want to find is inside msg.disease. As a result i want the key number of the object, i don't care for the array key. Thank you in advance for your time!

you could use a function node, call var myFlatDB = msg.dbase.flat() then use msg.payload = myFlatDB.includes('what i am looking for');

NOTE: this will only state true or false as to finding an exact match.

If you need to search partial or case in-sensitive and/or get the x/y index positions of the found item, then you will need to loop through each row (array) and then loop through each sub array & use regex or .includes() on each value

Here is an example

[{"id":"e7d8edec.a0fdd","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"8e2174d6.8ba838","type":"inject","z":"e7d8edec.a0fdd","name":"","props":[{"p":"dbase","v":"{\"0\":[\"test1\",\"test2\",\"test3\",\"test4\"],\"1\":[\"test5\"],\"2\":[\"test6\"],\"3\":[\"tesat7\"]}","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":120,"y":160,"wires":[["c54e814b.1a87"]]},{"id":"cc78d184.6d6a58","type":"debug","z":"e7d8edec.a0fdd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":160,"wires":[]},{"id":"c54e814b.1a87","type":"function","z":"e7d8edec.a0fdd","name":"","func":"msg.disease = \"test2\";\n\nfor (var key in msg.dbase) {\n   if(msg.dbase[key].includes(msg.disease)){\n       return {\"key\":key};\n   }\n}\nreturn;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":160,"wires":[["cc78d184.6d6a58"]]}]

As Steve said this will only work for exact match.


again the same thing as when i tried to loop through each row and sub array

what is in msg.dbase? My guess is nothing (hence the undefined error)

put a debug set to show complete message BEFORE the function - check msg.dbase is an array.

Yes its nothing but i created it with this function

Ahh just spotted - its and object with properties 0 and 1 which are arrays.

So flat wont work as expected (i think).

so you will need to get the keys var keys = Object.keys(msg.dbase); then loop through the array of keys and then search each array.

I can help if you knock up a demo flow with real data being injected (i.e. simulate your database using an inject)

  1. copy the msg.dbase data using the "Copy Value" button on the debug window
  2. paste the copied value into a change node that sets msg.dbase to the value you copied
  3. connect that to the function

You should end up with...

image

then export the partial flow and paste in a reply...
```
paste flow like this
```

Ah ha - YOU create the data & you have created a format difficult to search.

In that case - easy - dont create a difficult format of object with array props inside!!! - then finding is MUCH easier.

  • set msg.dbase = [] instead. Then your data will be an array.
  • next, add the conditions using [...conditions] (so you are adding ITEMS instead of adding sub-ARRAYS)

If you can generate dummy data for msg.payload.search_results I can show you how

my initial data is an xml file that i only keep the subfolder studies[i].conditions.condition and when i put conditions inside [.. ] it understands it as a variable that is not defined. i change the type of msg.dbase from obj to array but i cannot add items instead of sub-arrays

You can make a dummy flow with data inside (so I can simulate your situation) and I'll help - or - you can keep trying with the tips I've given you.

where file i put the directory of my xml file which i cannot send throw this message box, but i downloaded from here: https://clinicaltrials.gov/ct2/results?cond=&term=&cntry=&state=&city=&dist=
with the option xml and All Available Columns
thank you very much for your time!
i keep trying with the array and sub array format that i understand!

[{"id":"5921d952.b44428","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"ab957ec7.29539","type":"inject","z":"5921d952.b44428","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"Init","payloadType":"str","x":180,"y":240,"wires":[["e6ac2c8a.50e338"]]},{"id":"e6ac2c8a.50e338","type":"file in","z":"5921d952.b44428","name":"","filename":"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":310,"y":240,"wires":[["7b2344d.1aaf73c"]]},{"id":"7b2344d.1aaf73c","type":"xml","z":"5921d952.b44428","name":"","property":"payload","attr":"","chr":"","x":430,"y":240,"wires":[["5cfb50e6.458ac8"]]},{"id":"5cfb50e6.458ac8","type":"function","z":"5921d952.b44428","name":"","func":"msg.dbase = [];\nvar len = msg.payload.search_results.study.length;\nfor(var i=0;i<5;i++){\n    msg.dbase[i] = msg.payload.search_results.study[i].conditions[0].condition;\n}\nmsg.payload = msg.dbase[0];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":240,"wires":[["f27dd386.2468b8"]]},{"id":"f27dd386.2468b8","type":"debug","z":"5921d952.b44428","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"dbase","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":240,"wires":[]}]

Here you go ...

[{"id":"81d62991.c28128","type":"inject","z":"c479d7d3.fce8d8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"Init","payloadType":"str","x":150,"y":220,"wires":[["5c782e34.536cd"]]},{"id":"5c782e34.536cd","type":"file in","z":"c479d7d3.fce8d8","name":"","filename":"c:/temp/SearchResults.xml","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":350,"y":220,"wires":[["408f6d3b.017944"]]},{"id":"408f6d3b.017944","type":"xml","z":"c479d7d3.fce8d8","name":"","property":"payload","attr":"","chr":"","x":560,"y":220,"wires":[["5c6ece81.d698f","9f6eba98.0d1798"]]},{"id":"5c6ece81.d698f","type":"function","z":"c479d7d3.fce8d8","name":"","func":"var dbase = [];\nvar len = msg.payload.search_results.study.length;\nfor(var i=0;i<len;i++){\n    let data = msg.payload.search_results.study[i].conditions[0].condition;\n    dbase.push(...data);\n}\nmsg.payload = dbase;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":280,"wires":[["5ef24e86.83332"]]},{"id":"50ca8499.0587fc","type":"debug","z":"c479d7d3.fce8d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":280,"wires":[]},{"id":"9f6eba98.0d1798","type":"debug","z":"c479d7d3.fce8d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":220,"wires":[]},{"id":"5ef24e86.83332","type":"change","z":"c479d7d3.fce8d8","name":"","rules":[{"t":"set","p":"conditions","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":280,"wires":[["50ca8499.0587fc"]]},{"id":"ce4e8cef.b3c4a","type":"inject","z":"c479d7d3.fce8d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Anesthesia","payloadType":"str","x":160,"y":360,"wires":[["a328617b.422e"]]},{"id":"51739fff.279b6","type":"comment","z":"c479d7d3.fce8d8","name":"1. Load XML data, make flat array, store it in flow.conditions","info":"","x":310,"y":180,"wires":[]},{"id":"25e1145d.1ca0dc","type":"inject","z":"c479d7d3.fce8d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Food Habits","payloadType":"str","x":170,"y":400,"wires":[["a328617b.422e"]]},{"id":"3664026f.ed7f9e","type":"inject","z":"c479d7d3.fce8d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"_wont_find_me_","payloadType":"str","x":180,"y":480,"wires":[["a328617b.422e"]]},{"id":"744fa604.4422c8","type":"function","z":"c479d7d3.fce8d8","name":"exact search (exists true/false)","func":"var findWhat = msg.payload;\nvar conditions = msg.conditions;\nmsg.payload = conditions.includes(findWhat);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":610,"y":360,"wires":[["d11e148d.d87bb8"]]},{"id":"a328617b.422e","type":"change","z":"c479d7d3.fce8d8","name":"","rules":[{"t":"set","p":"conditions","pt":"msg","to":"conditions","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":360,"wires":[["744fa604.4422c8","d5a4ad29.38883","fbbe6b53.c33038","75b9828c.2c14bc","607e9988.3398c8"]]},{"id":"d5a4ad29.38883","type":"function","z":"c479d7d3.fce8d8","name":"partial search (exists true/false)","func":"var findWhat = msg.payload;\nvar conditions = msg.conditions;\nmsg.payload = conditions.some(e => e.includes(findWhat));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":610,"y":420,"wires":[["72e94dd8.cfab74"]]},{"id":"fbbe6b53.c33038","type":"function","z":"c479d7d3.fce8d8","name":"find partial (returns found item)","func":"var findWhat = msg.payload;\nvar conditions = msg.conditions;\nmsg.payload = conditions.find(e => e.includes(findWhat));\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":610,"y":540,"wires":[["2dc6d0cf.02dbb"]]},{"id":"d11e148d.d87bb8","type":"debug","z":"c479d7d3.fce8d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":950,"y":360,"wires":[]},{"id":"72e94dd8.cfab74","type":"debug","z":"c479d7d3.fce8d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":950,"y":420,"wires":[]},{"id":"2dc6d0cf.02dbb","type":"debug","z":"c479d7d3.fce8d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":950,"y":540,"wires":[]},{"id":"5df2abd6.e1bb94","type":"inject","z":"c479d7d3.fce8d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Quality Imp","payloadType":"str","x":170,"y":520,"wires":[["a328617b.422e"]]},{"id":"75b9828c.2c14bc","type":"function","z":"c479d7d3.fce8d8","name":"find partial (returns found item) (case insensitive)","func":"var findWhat = msg.payload.toLowerCase();\nvar conditions = msg.conditions;\nmsg.payload = conditions.find(e => e.toLowerCase().includes(findWhat));\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":660,"y":600,"wires":[["75c6f58f.475a5c"]]},{"id":"607e9988.3398c8","type":"function","z":"c479d7d3.fce8d8","name":"partial search (exists true/false) (case insensitive)","func":"var findWhat = msg.payload.toLowerCase();\nvar conditions = msg.conditions;\nmsg.payload = conditions.some(e => e.toLowerCase().includes(findWhat));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":670,"y":480,"wires":[["c8cd13ba.4b378"]]},{"id":"c8cd13ba.4b378","type":"debug","z":"c479d7d3.fce8d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":950,"y":480,"wires":[]},{"id":"75c6f58f.475a5c","type":"debug","z":"c479d7d3.fce8d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":950,"y":600,"wires":[]},{"id":"463f9ee6.77792","type":"comment","z":"c479d7d3.fce8d8","name":"2. Perform the search","info":"","x":180,"y":320,"wires":[]},{"id":"f4af84b8.e507b8","type":"inject","z":"c479d7d3.fce8d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"fOOd HaBiTs","payloadType":"str","x":170,"y":440,"wires":[["a328617b.422e"]]},{"id":"e3c813b1.cf5e7","type":"comment","z":"c479d7d3.fce8d8","name":"FIRST - change the file path to your XML file","info":"","x":470,"y":100,"wires":[]}]

thank you very much but even i took your flow exactly the same and i only changed the directory of the xml i get this error
image
it probably because the dbase doesn't iterate, but thank again for your help

did you press the top inject first?

Look in context you should see the data...

The fisrt inject loads the XML and converts it to a flat array (as I explained 3 posts ago, and as per my flow in the previous post) - then stores the flat array in flow context (as per picture above)

If you dont have that data then YES, includes will not be possible due to having no data to look through.

If in doubt - add a debug to EVERY node, press the inject, see where it fails.

EDIT...
Also, as you can see from my animation - it definitely works.

i copied exactly your flow, the database creation and insertion in the flow hapen as soon as i deploy, and i have the error, TypeError: data is not iterable (cannot read property undefined) also i don't understand how it enters the flow context in this code
image
yes this is the strange thing, i copied exactly your code and didn't change anything(exept the xml path)

Did you not simply import the flow?


Please answer the questions...

  • did you operate the first inject?
    image
    this is required to populate the context store

  • did you check context like i asked? Is there data in it?

  • which inject causes the error - top one or bottom one?

yes i imported it, yes i push the inject with Init in the label. The init node causes the error, i dont know how to check for the flow context but the error happens at the function node. i tried removing the three dots and the array showed as did before but with the three dots at the push command(dbase.push(...data))it shows the error that i mention above

What version of nodejs are you using?
What version of node-red are you using?

the spread operator ... has been available since node version 5.

it should not cause you an error unless the data format of the XML is different.

I literally posted a screen shot showing you how.

my context data is like before, without changing anything to your flow


my node version is 14.7.0