# Find object in an array, based on other input string

**URL:** <https://discourse.nodered.org/t/find-object-in-an-array-based-on-other-input-string/28349>\
**Category:** General\
**Created:** [15 June 2020 10:22 UTC](https://discourse.nodered.org/t/find-object-in-an-array-based-on-other-input-string/28349 "2020-06-15T10:22:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![namoora](https://avatars.discourse-cdn.com/v4/letter/n/4bbf92/32.png) [@namoora](https://discourse.nodered.org/u/namoora)\
**Post date:** [15 June 2020 10:22 UTC](https://discourse.nodered.org/t/find-object-in-an-array-based-on-other-input-string/28349/1 "2020-06-15T10:22:20Z")

</div>

Greetings together,

I've got the following situation:  
I'm trying to match a string with a corresponding object in an array and return this object as payload.

e.g. the string in msg.topic is `"-5421010250119599039-2006151152-6"` and the array is

msg.payload:

```auto
[
{"$":
{"id":"-7947913692768115784-2006151118-32","eva":"8004404"},
"ar":[{"$":{"ct":"2006151248","l":"2"}}],
"dp":[{"$":{"ct":"2006151249","l":"2"}}]},

{"$":{"id":"-5421010250119599039-2006151152-6","eva":"8004404"},
"ar":[{"$":{"ct":"2006151211","l":"2"}}],
"dp":[{"$":{"ct":"2006151212","l":"2"}}]},

{"$":{"id":"-2031174509165804068-2006151018-32","eva":"8004404"},
"ar":[{"$":{"ct":"2006151148","l":"2"}}],
"dp":[{"$":{"ct":"2006151149","l":"2"}}]},
]

```

I want to find the object in the string, where `msg.topic == msg.payload[i].$.id` and return `msg.payload[i].$.ar` or `msg.payload[i].$.dp`

I tought about using a function like

```auto
var id = msg.topic;
var payload = msg.payload;
var i;
var a;

for (i = 0; i < msg.payload.length; i++)
a = msg.payload(i).$.id;
if (a == id){

return msg;
}

```

But it did not work.

Any ideas on this?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 June 2020 10:42 UTC](https://discourse.nodered.org/t/find-object-in-an-array-based-on-other-input-string/28349/2 "2020-06-15T10:42:23Z")

</div>

You should be able to use [Array.find](https://www.w3schools.com/jsref/jsref_find.asp) to do that. It goes through the array, calling a function for each one, and returns the first matching element.

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [15 June 2020 11:57 UTC](https://discourse.nodered.org/t/find-object-in-an-array-based-on-other-input-string/28349/3 "2020-06-15T11:57:21Z")

</div>

You can also use a JSONata query in the change node to extract that information.

Here below the query for your problem.

[https://try.jsonata.org/eBEhhbcqs](https://try.jsonata.org/eBEhhbcqs)

```auto
payload["$".id = $$.topic]{
     "ar" : ar,
     "dp" : dp
}

```

Note that you need to put `$` between quotes in the payload filter as $ without quotes is a variable referring to the current context in a jsonata expression.

---

<div class="post-metadata">

**Author:** ![namoora](https://avatars.discourse-cdn.com/v4/letter/n/4bbf92/32.png) [@namoora](https://discourse.nodered.org/u/namoora)\
**Post date:** [15 June 2020 13:07 UTC](https://discourse.nodered.org/t/find-object-in-an-array-based-on-other-input-string/28349/4 "2020-06-15T13:07:52Z")

</div>

Thanks both.

Since msg..topic and msg.payload are from 2 different sources, I guess i must join them anyhow first?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/525ae082a6235b28a1da134a123daeabbd02729f.png)

```auto
[{"id":"34ef0afe.9a44b6","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"27433d2.7800bc2","type":"http request","z":"34ef0afe.9a44b6","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"https://api.deutschebahn.com/timetables/v1/fchg/8004404","tls":"","persist":false,"proxy":"","authType":"bearer","x":430,"y":100,"wires":[["33d6eb77.12a564"]]},{"id":"9d9966c7.d4f358","type":"inject","z":"34ef0afe.9a44b6","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":200,"wires":[["87feadb9.5b8dc","27433d2.7800bc2"]]},{"id":"b1b0b903.57d438","type":"http request","z":"34ef0afe.9a44b6","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"https://api.deutschebahn.com/timetables/v1/plan/8004404/{{{payload}}}","tls":"","persist":false,"proxy":"","authType":"bearer","x":410,"y":260,"wires":[["56dd9042.7d7d"]]},{"id":"87feadb9.5b8dc","type":"time-comp","z":"34ef0afe.9a44b6","outputs":1,"name":"","positionConfig":"aba1f65b.eda0b8","input":"","inputType":"date","inputFormat":"0","inputOffset":0,"inputOffsetType":"none","inputOffsetMultiplier":60000,"rules":[],"checkall":"true","result1":"","result1Type":"msgPayload","result1Value":"","result1ValueType":"input","result1Format":"yyMMdd/HH","result1Offset":0,"result1OffsetType":"none","result1OffsetMultiplier":60000,"x":200,"y":260,"wires":[["b1b0b903.57d438"]]},{"id":"56dd9042.7d7d","type":"html","z":"34ef0afe.9a44b6","name":"","property":"payload","outproperty":"payload","tag":"s","ret":"attr","as":"single","x":630,"y":220,"wires":[["236b8ce3.fc1f54"]]},{"id":"236b8ce3.fc1f54","type":"change","z":"34ef0afe.9a44b6","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload[0].id","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":180,"wires":[[]]},{"id":"33d6eb77.12a564","type":"xml","z":"34ef0afe.9a44b6","name":"","property":"payload","attr":"","chr":"","x":630,"y":100,"wires":[["73948ba8.543a14"]]},{"id":"73948ba8.543a14","type":"change","z":"34ef0afe.9a44b6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.timetable.s","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":100,"wires":[[]]},{"id":"ad94e4f1.6168f8","type":"change","z":"34ef0afe.9a44b6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[\"$\".id = $$.topic]{ \"ar\" : ar, \"dp\" : dp }","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1110,"y":160,"wires":[[]]},{"id":"626820e1.c325d","type":"function","z":"34ef0afe.9a44b6","name":"find","func":"var id = msg.topic;\nvar payload = [msg.payload[0], msg.payload[1], msg.payload[2], msg.payload[3]];\n\nfunction findTrain(payload){\n return payload == id;\n}\n\nfunction myFunction(){\n document.getElementById(\"$.id\").innerHTML = payload.find(findTrain);\n}\n\nmsg = payload;\nreturn msg;\n","outputs":1,"noerr":0,"x":1070,"y":120,"wires":[[]]},{"id":"aba1f65b.eda0b8","type":"position-config","z":"","name":"Niederroth","isValide":"true","longitude":"0","latitude":"0","angleType":"deg","timeZoneOffset":"99","timeZoneDST":"0","stateTimeFormat":"3","stateDateFormat":"12"}]

```

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [15 June 2020 13:11 UTC](https://discourse.nodered.org/t/find-object-in-an-array-based-on-other-input-string/28349/5 "2020-06-15T13:11:20Z")

</div>

You have launched the http requests in parallel.  
I think it is better to launch the http requests in series then you don't need to join.

---

<div class="post-metadata">

**Author:** ![namoora](https://avatars.discourse-cdn.com/v4/letter/n/4bbf92/32.png) [@namoora](https://discourse.nodered.org/u/namoora)\
**Post date:** [15 June 2020 15:17 UTC](https://discourse.nodered.org/t/find-object-in-an-array-based-on-other-input-string/28349/6 "2020-06-15T15:17:08Z")

</div>

Thank you both,

I have used the solution of @janvda.

The whole thing looks not quite complex 🙂

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/47bb373311876b3c3a4148fb3a51dbbbe303bc2e.png)

---

<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:** [29 June 2020 15:17 UTC](https://discourse.nodered.org/t/find-object-in-an-array-based-on-other-input-string/28349/7 "2020-06-29T15:17:23Z")

</div>

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