# Search for value in an array

**URL:** <https://discourse.nodered.org/t/search-for-value-in-an-array/51733>\
**Category:** General\
**Created:** [30 September 2021 20:21 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733 "2021-09-30T20:21:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)\
**Post date:** [30 September 2021 20:21 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/1 "2021-09-30T20:21:50Z")

</div>

hello, i am trying a quick and easy way to look up a value in an array.  
I know who he discussed several times on the forum on the subject, but I did not find my happiness.  
my array contains 2 columns and 38,000 rows. I would like to retrieve a value from columns 2 following a variable from column 2.I found a solution on the forum in Json, but the flow takes 1 minute to find the solution.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c2350ff6333d5c7d7f140544fdc0c7b7ac5d294.png)  
I would like to find a solution in javascript in a function node to find my value of column 2 based on variable of column 1  
Merci d'avance.

---

<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:** [30 September 2021 20:34 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/2 "2021-09-30T20:34:56Z")

</div>

Can you show us some sample data please? Also exactly what you want to get from that data.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [30 September 2021 20:35 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/3 "2021-09-30T20:35:00Z")

</div>

The "row" and "column" are not terms of array elements thus your description of problem can't be read out in clear way.  
It helps if you can make small example of your data structure (couple of elements is enough).  
Then there will be no guessing

---

<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:** [30 September 2021 20:36 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/4 "2021-09-30T20:36:34Z")

</div>

In fact preferably share a flow consisting of an Inject node that injects the data so we can test our solutions.

---

<div class="post-metadata">

**Author:** ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)\
**Post date:** [30 September 2021 21:15 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/5 "2021-09-30T21:15:01Z")

</div>

[{"id":"698ae74b.381e38","type":"file","z":"2bc0ec57.eb2a94","name":"","filename":"/home/pi/logs/PlaningSolar","appendNewline":true,"createDir":false,"overwriteFile":"true","x":1089,"y":233,"wires":[]},{"id":"99a6611f.8909","type":"template","z":"2bc0ec57.eb2a94","name":"simulate csv file","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"1x1x0x0,0\n1x1x0x15,0\n1x1x0x30,0\n1x1x0x45,0\n1x1x1x0,0\n1x1x1x15,0\n1x1x1x30,0\n1x1x1x45,0\n1x1x2x0,0\n1x1x2x15,0\n","output":"str","x":802,"y":232,"wires":[["698ae74b.381e38"]]},{"id":"f3792806.b83398","type":"csv","z":"2bc0ec57.eb2a94","name":"","sep":",","hdrin":false,"hdrout":true,"multi":"mult","ret":"\n","temp":"","skip":"0","x":650,"y":548,"wires":[["e14bca0c.ec1f48"]]},{"id":"786d8a25.ed7f44","type":"debug","z":"2bc0ec57.eb2a94","name":"11","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","x":1127,"y":580,"wires":},{"id":"179b17.06e1a4ea","type":"file in","z":"2bc0ec57.eb2a94","name":"","filename":"/home/pi/logs/PlaningSolar","format":"utf8","chunk":false,"sendError":false,"x":415,"y":554,"wires":[["f3792806.b83398"]]},{"id":"e73a973e.9a39c8","type":"inject","z":"2bc0ec57.eb2a94","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":147,"y":555,"wires":[["179b17.06e1a4ea"]]},{"id":"e14bca0c.ec1f48","type":"function","z":"2bc0ec57.eb2a94","name":"66","func":"msg.part\_number = flow.get('part')||0;\n\nreturn msg;","outputs":1,"noerr":0,"x":781,"y":550,"wires":[["3cfad59f.9b411a"]]},{"id":"7b23d94d.e45378","type":"inject","z":"2bc0ec57.eb2a94","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":600,"y":232,"wires":[["99a6611f.8909"]]},{"id":"3cfad59f.9b411a","type":"change","z":"2bc0ec57.eb2a94","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[col1=$string($$.part\_number)].col2","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":965,"y":567,"wires":[["786d8a25.ed7f44"]]},{"id":"f53dda7d.2fa3c8","type":"inject","z":"2bc0ec57.eb2a94","name":"","topic":"","payload":"8x22x9x45","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":164,"y":246,"wires":[["45dab645.53c998"]]},{"id":"45dab645.53c998","type":"function","z":"2bc0ec57.eb2a94","name":"55","func":"flow.set('part',msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":335,"y":247,"wires":[]},{"id":"ede2d6ba.1de2e8","type":"comment","z":"2bc0ec57.eb2a94","name":"variable de la colone1","info":"","x":195,"y":187,"wires":},{"id":"c4e235bf.30aa58","type":"comment","z":"2bc0ec57.eb2a94","name":"planning tracker solaire","info":"","x":805,"y":183,"wires":},{"id":"2478b9bd.665596","type":"comment","z":"2bc0ec57.eb2a94","name":"start the search in column 2","info":"","x":420,"y":500,"wires":}]

here is the flow,

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [30 September 2021 21:18 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/6 "2021-09-30T21:18:07Z")

</div>

@fabien Your flow above is not usable.  
Please see [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) and then use the pencil icon to edit your last post so that others can load the flow, and help you.

---

<div class="post-metadata">

**Author:** ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)\
**Post date:** [30 September 2021 21:19 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/7 "2021-09-30T21:19:50Z")

</div>

`````auto
type or paste code here
````[{"id":"698ae74b.381e38","type":"file","z":"2bc0ec57.eb2a94","name":"","filename":"/home/pi/logs/PlaningSolar","appendNewline":true,"createDir":false,"overwriteFile":"true","x":1089,"y":233,"wires":[[]]},{"id":"99a6611f.8909","type":"template","z":"2bc0ec57.eb2a94","name":"simulate csv file","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"1x1x0x0,0\n1x1x0x15,0\n1x1x0x30,0\n1x1x0x45,0\n1x1x1x0,0\n1x1x1x15,0\n1x1x1x30,0\n1x1x1x45,0\n1x1x2x0,0\n1x1x2x15,0\n","output":"str","x":802,"y":232,"wires":[["698ae74b.381e38"]]},{"id":"f3792806.b83398","type":"csv","z":"2bc0ec57.eb2a94","name":"","sep":",","hdrin":false,"hdrout":true,"multi":"mult","ret":"\\n","temp":"","skip":"0","x":650,"y":548,"wires":[["e14bca0c.ec1f48"]]},{"id":"786d8a25.ed7f44","type":"debug","z":"2bc0ec57.eb2a94","name":"11","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","x":1127,"y":580,"wires":[]},{"id":"179b17.06e1a4ea","type":"file in","z":"2bc0ec57.eb2a94","name":"","filename":"/home/pi/logs/PlaningSolar","format":"utf8","chunk":false,"sendError":false,"x":415,"y":554,"wires":[["f3792806.b83398"]]},{"id":"e73a973e.9a39c8","type":"inject","z":"2bc0ec57.eb2a94","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":147,"y":555,"wires":[["179b17.06e1a4ea"]]},{"id":"e14bca0c.ec1f48","type":"function","z":"2bc0ec57.eb2a94","name":"66","func":"msg.part_number = flow.get('part')||0;\n\nreturn msg;","outputs":1,"noerr":0,"x":781,"y":550,"wires":[["3cfad59f.9b411a"]]},{"id":"7b23d94d.e45378","type":"inject","z":"2bc0ec57.eb2a94","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":600,"y":232,"wires":[["99a6611f.8909"]]},{"id":"3cfad59f.9b411a","type":"change","z":"2bc0ec57.eb2a94","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[col1=$string($$.part_number)].col2","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":965,"y":567,"wires":[["786d8a25.ed7f44"]]},{"id":"f53dda7d.2fa3c8","type":"inject","z":"2bc0ec57.eb2a94","name":"","topic":"","payload":"8x22x9x45","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":164,"y":246,"wires":[["45dab645.53c998"]]},{"id":"45dab645.53c998","type":"function","z":"2bc0ec57.eb2a94","name":"55","func":"flow.set('part',msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":335,"y":247,"wires":[[]]},{"id":"ede2d6ba.1de2e8","type":"comment","z":"2bc0ec57.eb2a94","name":"variable de la colone1","info":"","x":195,"y":187,"wires":[]},{"id":"c4e235bf.30aa58","type":"comment","z":"2bc0ec57.eb2a94","name":"planning tracker solaire","info":"","x":805,"y":183,"wires":[]},{"id":"2478b9bd.665596","type":"comment","z":"2bc0ec57.eb2a94","name":"start the search in column 2","info":"","x":420,"y":500,"wires":[]}]`
`````

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [30 September 2021 21:23 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/8 "2021-09-30T21:23:07Z")

</div>

Please follow the example that I linked to.  
The three (not 4) backticks need to be on their own lines, and not as you have posted.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [30 September 2021 21:48 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/9 "2021-09-30T21:48:40Z")

</div>

Hi Fabien,

When pasting flows (besides pasting it correctly) try to make it as simple as possible  
you dont need to include the writing / reading of the file.

regarding your question, you could use in your Function node the [javascript find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) method to search though the array (if you are looking for a single match. for many use `.filter`)

**Test Flow:**

```auto
[{"id":"99a6611f.8909","type":"template","z":"5847b7aa62131d37","name":"simulate csv file","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"1x1x0x0,0\n1x1x0x15,0\n1x1x0x30,0\n1x1x0x45,0\n1x1x1x0,0\n1x1x1x15,0\n1x1x1x30,0\n1x1x1x45,0\n1x1x2x0,0\n1x1x2x15,0\n","output":"str","x":360,"y":480,"wires":[["f3792806.b83398","4fd0634cb5a109be"]]},{"id":"f3792806.b83398","type":"csv","z":"5847b7aa62131d37","name":"","sep":",","hdrin":false,"hdrout":"all","multi":"mult","ret":"\\n","temp":"part, value","skip":"0","strings":true,"include_empty_strings":false,"include_null_values":false,"x":550,"y":480,"wires":[["e14bca0c.ec1f48","b26e9ea0598f45bf"]]},{"id":"786d8a25.ed7f44","type":"debug","z":"5847b7aa62131d37","name":"11","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":890,"y":480,"wires":[]},{"id":"e14bca0c.ec1f48","type":"function","z":"5847b7aa62131d37","name":"find","func":"let part = flow.get('part')\n\nif (part) {\n msg.payload = msg.payload.find(el => el.part == part)\n return msg;\n}\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":480,"wires":[["786d8a25.ed7f44"]]},{"id":"7b23d94d.e45378","type":"inject","z":"5847b7aa62131d37","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":158,"y":480,"wires":[["99a6611f.8909"]]},{"id":"f53dda7d.2fa3c8","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1x1x1x45","payloadType":"str","x":155,"y":273,"wires":[["45dab645.53c998"]]},{"id":"45dab645.53c998","type":"function","z":"5847b7aa62131d37","name":"set part","func":"flow.set('part',msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":336,"y":274,"wires":[[]]},{"id":"ede2d6ba.1de2e8","type":"comment","z":"5847b7aa62131d37","name":"variable de la colone1","info":"","x":186,"y":214,"wires":[]},{"id":"c4e235bf.30aa58","type":"comment","z":"5847b7aa62131d37","name":"planning tracker solaire","info":"","x":240,"y":420,"wires":[]},{"id":"4fd0634cb5a109be","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":400,"wires":[]},{"id":"b26e9ea0598f45bf","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":400,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![fabien](https://avatars.discourse-cdn.com/v4/letter/f/85e7bf/32.png) [@fabien](https://discourse.nodered.org/u/fabien)\
**Post date:** [2 October 2021 14:14 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/10 "2021-10-02T14:14:13Z")

</div>

thank you, this is exactly the function I am looking for

---

<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:** [1 December 2021 14:14 UTC](https://discourse.nodered.org/t/search-for-value-in-an-array/51733/11 "2021-12-01T14:14:53Z")

</div>

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