Search array and return index (key)

{
  "Name": "MACRO NAME",
  "CreationDate": "2020-8-13",
  "Commands": [
    {
      "Command": "csvReadArray",
      "Target": "csvName.csv",
      "Value": "arrName"
    },
    {
      "Command": "store",
      "Target": "test string",
      "Value": "str"
    },
    {
      "Command": "executeScript",
      "Target": "function arrSearch(query,arr){ var query = query.trim(); for(var z=0;z<arr.length;z++){if(arr[z].indexOf(query)!==-1){return z;break;}} return-1;} var q=arrSearch(${str},${arrName}); return q;",
      "Value": "search"
    },
    {
      "Command": "echo",
      "Target": "<!--- RESULTS FROM SEARCH ARRAY :   ${search}",
      "Value": ""
    }
  ]
}

Is this post a question or a working demo? :wink:

Working demo, a snippet to show it can be done, the full code can’t be disclosed.

The original use case called for looping through a csv and doing a string comparison, searching an array is much faster, so after the search is complete and match is made, the key or index is returned; I used that returned index to get the value arr[index] from the array.

Much faster searching and in my case I removed and unnecessary loop from the json object.

1 Like