# Search inside an object of arrays

**URL:** <https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916>\
**Category:** General\
**Created:** [30 July 2020 09:39 UTC](https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916 "2020-07-30T09:39:01Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 July 2020 15:30 UTC](https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916/21 "2020-07-30T15:30:22Z")

</div>

> [@Bernnt](#):
>
> my node version is 14.7.0

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...

```auto
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

---

<div class="post-metadata">

**Author:** ![Bernnt](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@Bernnt](https://discourse.nodered.org/u/Bernnt)\
**Post date:** [30 July 2020 15:35 UTC](https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916/22 "2020-07-30T15:35:59Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> 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;
> 
> ```

yes that was it, here my context data

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e314c2fa22609bf2cd13175c0e232b5765ba4e12.png)  
just one more question if i want the key of the value that i search what i have to change?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 July 2020 15:38 UTC](https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916/23 "2020-07-30T15:38:40Z")

</div>

> [@Bernnt](#):
>
> 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?

---

<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:** [30 July 2020 15:41 UTC](https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916/24 "2020-07-30T15:41:18Z")

</div>

> [@Bernnt](#):
>
> As a result i want the key number of the object, i don't care for the array key

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

---

<div class="post-metadata">

**Author:** ![Bernnt](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@Bernnt](https://discourse.nodered.org/u/Bernnt)\
**Post date:** [30 July 2020 15:41 UTC](https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916/25 "2020-07-30T15:41:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 July 2020 16:01 UTC](https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916/26 "2020-07-30T16:01:53Z")

</div>

> [@Bernnt](#):
>
> As a result i want the key number of the object

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...

> [@Steve-Mcl](#):
>
> 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!!!

Never mind.

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 July 2020 17:12 UTC](https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916/27 "2020-07-30T17:12:22Z")

</div>

@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...

```auto
[{"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":[]}]

```

 ![0DdFFY1ysT](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/43d6a3627e7a9a6198f37d55d32797db58d058be.gif)

---

<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:** [13 August 2020 17:12 UTC](https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916/28 "2020-08-13T17:12:26Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/search-inside-an-object-of-arrays/30916.md?page=1)
