Search for value in an array

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.


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.

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

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

1 Like

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

[{"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,

@fabien Your flow above is not usable.
Please see How to share code or flow json and then use the pencil icon to edit your last post so that others can load the flow, and help you.

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

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.

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 method to search though the array (if you are looking for a single match. for many use .filter)

Test Flow:

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

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

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