# Chercher dans un tableau

**URL:** <https://discourse.nodered.org/t/chercher-dans-un-tableau/21342>\
**Category:** General\
**Created:** [6 February 2020 09:53 UTC](https://discourse.nodered.org/t/chercher-dans-un-tableau/21342 "2020-02-06T09:53:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![micke007](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@micke007](https://discourse.nodered.org/u/micke007)\
**Post date:** [6 February 2020 09:53 UTC](https://discourse.nodered.org/t/chercher-dans-un-tableau/21342/1 "2020-02-06T09:53:01Z")

</div>

bonjour a tous  
je cherche a trouver le prix le moins cher et afficher les informations qui vont avec

```auto
[{"id":"c2d7159b.44db98","type":"http request","z":"3dafcb6f.c00894","name":"1","method":"GET","ret":"txt","paytoqs":false,"url":"https://data.orleans-metropole.fr/api/records/1.0/search/?dataset=prix-des-carburants-j-7&facet=city&facet=fuel&facet=brand&facet=address&facet=services","tls":"","persist":false,"proxy":"","authType":"","x":270,"y":80,"wires":[["475e4c66.af1754"]]}]

```

j'ai trouver comment chercher le moins cher avec ça

```auto
[{"id":"d2d980d.a38238","type":"inject","z":"3dafcb6f.c00894","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":200,"wires":[["4d7fd001.3647f"]]},{"id":"4d7fd001.3647f","type":"http request","z":"3dafcb6f.c00894","name":"1","method":"GET","ret":"txt","paytoqs":false,"url":"https://data.orleans-metropole.fr/api/records/1.0/search/?dataset=prix-des-carburants-j-7&facet=cp&facet=pop&facet=city&facet=automate_24_24&facet=fuel&facet=shortage&facet=update&facet=services&facet=brand","tls":"","persist":false,"proxy":"","authType":"","x":230,"y":200,"wires":[["96796bac.bf8dc8"]]},{"id":"96796bac.bf8dc8","type":"json","z":"3dafcb6f.c00894","name":"","property":"payload","action":"","pretty":false,"x":350,"y":200,"wires":[["1de7d98e.a43726"]]},{"id":"1de7d98e.a43726","type":"function","z":"3dafcb6f.c00894","name":"var","func":"for (i=0; i<=8; i++) {\nvar out = msg.payload.records[i].fields.price_gazole;\nnode.send({payload: out});\n}\n","outputs":1,"noerr":0,"x":470,"y":200,"wires":[["dbbcbaa0.68ddd8"]]},{"id":"dbbcbaa0.68ddd8","type":"smooth","z":"3dafcb6f.c00894","name":"","property":"payload","action":"min","count":"9","round":"3","mult":"single","reduce":true,"x":620,"y":200,"wires":[["cd059816.f02aa8"]]},{"id":"cd059816.f02aa8","type":"debug","z":"3dafcb6f.c00894","name":"test","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":770,"y":200,"wires":[]}]

```

mais comment faire pour avoir toutes les information qui vont avec  
j'ai fait plein de recherche, plein d'essais mais je ne trouve rien  
je suis a la recherche d'idées nouvelles  
merci a vous

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [6 February 2020 22:13 UTC](https://discourse.nodered.org/t/chercher-dans-un-tableau/21342/2 "2020-02-06T22:13:28Z")

</div>

You may want to try this flow. I will be glad to explain how it works but please confirm it does what you want.

```auto
[{"id":"89e74a75.a195d8","type":"inject","z":"b13d121c.2a569","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":640,"wires":[["2b06e9d5.adf616"]]},{"id":"2b06e9d5.adf616","type":"http request","z":"b13d121c.2a569","name":"1","method":"GET","ret":"txt","paytoqs":false,"url":"https://data.orleans-metropole.fr/api/records/1.0/search/?dataset=prix-des-carburants-j-7&facet=cp&facet=pop&facet=city&facet=automate_24_24&facet=fuel&facet=shortage&facet=update&facet=services&facet=brand","tls":"","persist":false,"proxy":"","authType":"","x":390,"y":640,"wires":[["2363d903.e0f3d6"]]},{"id":"2363d903.e0f3d6","type":"json","z":"b13d121c.2a569","name":"","property":"payload","action":"","pretty":false,"x":510,"y":640,"wires":[["f51e604a.4742a"]]},{"id":"f363e1f6.1a465","type":"debug","z":"b13d121c.2a569","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1250,"y":740,"wires":[]},{"id":"f51e604a.4742a","type":"change","z":"b13d121c.2a569","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.records","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":640,"wires":[["5af8c590.f8d80c","4dbfdee1.c2429"]]},{"id":"67cf04c3.fd3dec","type":"sort","z":"b13d121c.2a569","name":"","order":"ascending","as_num":false,"target":"","targetType":"seq","msgKey":"","msgKeyType":"elem","seqKey":"payload.fields.price_gazole","seqKeyType":"msg","x":970,"y":740,"wires":[["680f224.8944cdc"]]},{"id":"5af8c590.f8d80c","type":"debug","z":"b13d121c.2a569","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.fields.price_gazole","targetType":"jsonata","x":900,"y":580,"wires":[]},{"id":"4dbfdee1.c2429","type":"split","z":"b13d121c.2a569","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":830,"y":740,"wires":[["67cf04c3.fd3dec"]]},{"id":"680f224.8944cdc","type":"switch","z":"b13d121c.2a569","name":"","property":"payload","propertyType":"msg","rules":[{"t":"head","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1110,"y":740,"wires":[["f363e1f6.1a465"]]}]

```

---

<div class="post-metadata">

**Author:** ![micke007](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@micke007](https://discourse.nodered.org/u/micke007)\
**Post date:** [7 February 2020 12:58 UTC](https://discourse.nodered.org/t/chercher-dans-un-tableau/21342/3 "2020-02-07T12:58:13Z")

</div>

Thank you so so much! I've been looking for this for weeks! It will be wonderfull if you could explain some things, like what is the last "switch node"? What is it for?  
It's amazing, thanks again!

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [7 February 2020 15:50 UTC](https://discourse.nodered.org/t/chercher-dans-un-tableau/21342/4 "2020-02-07T15:50:14Z")

</div>

Super. I am happy it works. So, explaining:

 ![a-1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e03545084df85ea48a5a8131bf2e24a7c1568c4.png)

1- Stores in msg.payload the array with the interesting information.  
2- Split each element of the array into a separate message, so we can modify, transform, sort, filter, etc.  
3- The sort node has an internal buffer that will collect all individual pieces and will output them (individually) sorted based on a property of the message (gas price in our case).  
4- The switch node here is working as a filter. It will pass only the head message, that is the first one from the sequence. As the sequence was sorted in ascendant order we will get only the message that has the lower gas price.

Message sequences is an effective way to transform data without using explicit JavaScript loops.

There is an alternative that is the use of JSONata expressions. It allows to transform complex data sets but comes with a price. JSONata is hard to learn and easy to forget (at least that is my case).

Using JSONata we could get a shorter flow.

 ![a-2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/eae5aee7f138cc7828a8bbdc13cf10103b9d1695.png)

```auto
[{"id":"4a48be2a.5cbd2","type":"inject","z":"b13d121c.2a569","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1040,"wires":[["7ce4cec1.54f97"]]},{"id":"4fd2eff1.4835c","type":"http request","z":"b13d121c.2a569","name":"http request","method":"GET","ret":"obj","paytoqs":true,"url":"https://data.orleans-metropole.fr/api/records/1.0/search/","tls":"","persist":false,"proxy":"","authType":"","x":570,"y":1040,"wires":[["d7e67d42.11467"]]},{"id":"d7e67d42.11467","type":"change","z":"b13d121c.2a569","name":"","rules":[{"t":"set","p":"sorted","pt":"msg","to":"payload.records^(fields.price_gazole)[0]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":1040,"wires":[["a435a67a.58e988"]]},{"id":"a435a67a.58e988","type":"debug","z":"b13d121c.2a569","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"sorted","targetType":"msg","x":930,"y":1040,"wires":[]},{"id":"eb155cc5.eabcd","type":"comment","z":"b13d121c.2a569","name":"Using JSONata only","info":"","x":210,"y":980,"wires":[]},{"id":"7ce4cec1.54f97","type":"change","z":"b13d121c.2a569","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"dataset\":\"prix-des-carburants-j-7\",\"facet\":\"brand\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":1040,"wires":[["4fd2eff1.4835c"]]}]

```

---

<div class="post-metadata">

**Author:** ![micke007](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@micke007](https://discourse.nodered.org/u/micke007)\
**Post date:** [7 February 2020 17:57 UTC](https://discourse.nodered.org/t/chercher-dans-un-tableau/21342/5 "2020-02-07T17:57:45Z")

</div>

Amazing, everything's clearer now. Thank you, it's really great. Gonna work on it ASAP.  
Have a nice weekend!

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [8 February 2020 08:15 UTC](https://discourse.nodered.org/t/chercher-dans-un-tableau/21342/6 "2020-02-08T08:15:29Z")

</div>

> [@Andrei](#):
>
> JSONata is hard to learn and easy to forget (at least that is my case).

I feel less lonely... 😉

GV

---

<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:** [8 April 2020 08:15 UTC](https://discourse.nodered.org/t/chercher-dans-un-tableau/21342/7 "2020-04-08T08:15:29Z")

</div>

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