Search inside an object of arrays

So the spread operator IS supported - something else is happening.

currently, as you have removed the spread operator push(...data) you are adding arrays to each array element. That basically breaks the search routines I wrote.

the issue...

my guess is one or more elements in the XML are empty (so the spread operator fails)

the fix...

try replacing the code in the top function node (the function in the top flow) with this...

var dbase = [];
var len = msg.payload.search_results.study.length;
for(var i=0;i<len;i++){
    try {
        let data = msg.payload.search_results.study[i].conditions[0].condition;
        dbase.push(...data);
    } catch(e) {
        //do nothing
    }
}
msg.payload = dbase;
return msg;

THEN...

click the init

then

check the data in context again (screen shot it) (dont forget to press the REFRESH button on the context viewer)

then

try the search functions again

1 Like

yes that was it, here my context data


just one more question if i want the key of the value that i search what i have to change?

Funny thing is, i knew back at about the 3rd post you would need more than just "finding" an item

You really should have stated EXACTLY what you wanted as that changes quite a lot.

What EXACTLY do you want once it is found?

@Steve-Mcl, he did state it in the first post.

i said it exaclty in my first post, i want to find the key so then i can extract more information for the specific, so i thought the best thing was to find the key and then knowing exactly where my data is extracting it and sending it to my website as a search result

there is no key number - there is either a reference to the original object/property in the data OR you store the interseting data in the flat array. That was my point.

For example, once you have the "key" (whatever that is) how would you access the extra data?

the better solution is to simplt find the value in the ORIGINAL data and return the object (then you will have full access to the items inside.

That is why I said...

Never mind.

I have a better solution - but I have to nip out for 30 mins

@Bernnt

Try this version instead - hopefully it is closer to your requirements

It doesnt mangle or flatten the original data (its stored in original format in flow context)

The search routines search the original data.

The return is always an array containing all objects found that match your search criteria.

If you need a specific item from the result, find it in the debug msg, use the "copy path" button

flow...

[{"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":260,"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":260,"wires":[["408f6d3b.017944"]]},{"id":"408f6d3b.017944","type":"xml","z":"c479d7d3.fce8d8","name":"","property":"payload","attr":"","chr":"","x":560,"y":260,"wires":[["5ef24e86.83332"]]},{"id":"5ef24e86.83332","type":"change","z":"c479d7d3.fce8d8","name":"","rules":[{"t":"set","p":"studydata","pt":"flow","to":"payload.search_results.study","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":260,"wires":[[]]},{"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, store it in flow.conditions","info":"","x":240,"y":220,"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 data = msg.studydata;\nmsg.payload = data.filter(e => {\n    try {\n        var conditions = e.conditions[0].condition\n        if(conditions.includes(findWhat)) {\n            return true;\n        }\n    } catch {\n        \n    }\n    return false\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":650,"y":360,"wires":[["d11e148d.d87bb8","30041c9c.853d44"]]},{"id":"a328617b.422e","type":"change","z":"c479d7d3.fce8d8","name":"","rules":[{"t":"set","p":"studydata","pt":"msg","to":"studydata","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":360,"wires":[["744fa604.4422c8","d5a4ad29.38883","607e9988.3398c8","48bbe978.ef6f98"]]},{"id":"d5a4ad29.38883","type":"function","z":"c479d7d3.fce8d8","name":"partial search (exists true/false)","func":"\nvar findWhat = msg.payload;\nvar data = msg.studydata;\nmsg.payload = data.filter(e => {\n    try {\n        var conditions = e.conditions[0].condition;\n        var found = conditions.some(e => {\n            return (e && e.length) ? e.includes(findWhat) : false\n        })\n        return found;\n    } catch {\n    }\n    return null\n});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":650,"y":480,"wires":[["72e94dd8.cfab74"]]},{"id":"d11e148d.d87bb8","type":"debug","z":"c479d7d3.fce8d8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload[0].title","targetType":"msg","statusVal":"payload","statusType":"auto","x":1010,"y":360,"wires":[]},{"id":"72e94dd8.cfab74","type":"debug","z":"c479d7d3.fce8d8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload[0].title","targetType":"msg","statusVal":"payload","statusType":"auto","x":1010,"y":480,"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":"607e9988.3398c8","type":"function","z":"c479d7d3.fce8d8","name":"partial search (exists true/false) (case insensitive)","func":"var findWhat = msg.payload.toLowerCase();\nvar data = msg.studydata;\nmsg.payload = data.filter(e => {\n    try {\n        var conditions = e.conditions[0].condition;\n        var found = conditions.some(e => {\n            return (e && e.length) ? e.toLowerCase().includes(findWhat) : false\n        })\n        return found;\n    } catch {\n    }\n    return null\n});\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":710,"y":540,"wires":[["c8cd13ba.4b378","7c039174.c795d"]]},{"id":"c8cd13ba.4b378","type":"debug","z":"c479d7d3.fce8d8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload[0].title","targetType":"msg","statusVal":"payload","statusType":"auto","x":1010,"y":540,"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":250,"y":180,"wires":[]},{"id":"48bbe978.ef6f98","type":"function","z":"c479d7d3.fce8d8","name":"exact search (exists true/false) (case insensitive)","func":"var findWhat = msg.payload.toLowerCase();\nvar data = msg.studydata;\nmsg.payload = data.filter(e => {\n    try {\n        var conditions = e.conditions[0].condition;\n        var found = conditions.some(e => {\n            return (e && e.length) ? e.toLowerCase() === findWhat : false\n        })\n        return found;\n    } catch {\n    }\n    return null\n});\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":420,"wires":[["c7354317.cf208"]]},{"id":"c7354317.cf208","type":"debug","z":"c479d7d3.fce8d8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload[0].title","targetType":"msg","statusVal":"payload","statusType":"auto","x":1010,"y":420,"wires":[]},{"id":"9fc765ca.fac068","type":"comment","z":"c479d7d3.fce8d8","name":"This version doesnt flatten the original data - instead, it searches the original data and returns ANY item it finds","info":"","x":450,"y":60,"wires":[]},{"id":"2fdc2330.67c33c","type":"comment","z":"c479d7d3.fce8d8","name":"That way, you have full access to ALL items related to the found text","info":"","x":320,"y":100,"wires":[]},{"id":"e71be32f.9d4ab","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":160,"y":560,"wires":[["a328617b.422e"]]},{"id":"30041c9c.853d44","type":"switch","z":"c479d7d3.fce8d8","name":"Found?","property":"payload","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":870,"y":320,"wires":[["19b7b63f.b0b46a"]]},{"id":"19b7b63f.b0b46a","type":"debug","z":"c479d7d3.fce8d8","name":"first found item","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload[0]","targetType":"msg","statusVal":"","statusType":"auto","x":1040,"y":320,"wires":[]},{"id":"7c039174.c795d","type":"switch","z":"c479d7d3.fce8d8","name":"Found?","property":"payload","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":860,"y":600,"wires":[["f6d949dd.411978"]]},{"id":"f6d949dd.411978","type":"debug","z":"c479d7d3.fce8d8","name":"first found item","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload[0]","targetType":"msg","statusVal":"","statusType":"auto","x":1030,"y":600,"wires":[]}]

1 Like

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