Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find element in array using regex match without iterating over array

I have an array of elements

["page=4", "sortOrder=asc", "datePosted=all-time"]

Using javascript or jquery I want to find the index of the element which begins "sortOrder=" . I will not know the full string of this element at compile time, just the "sortOrder=" part.

I'm assuming that this can be done without the need to iterate over the array and perform item.match("sortOrder="), but maybe I am wrong.

Thanks for any help.

like image 544
endorphin Avatar asked Aug 12 '11 10:08

endorphin


4 Answers

If you want to get the value inside an array that matches a specific regex:

...to get the value of the first match only, you can use find()

const array = ["page=4", "sortOrder=asc", "datePosted=all-time", "sortOrder=desc"];
const match = array.find(value => /^sortOrder=/.test(value));
// match = "sortOrder=asc"

...to get an array of all match results, you can use filter()

const array = ["page=4", "sortOrder=asc", "datePosted=all-time", "sortOrder=desc"];
const matches = array.filter(value => /^sortOrder=/.test(value));
// matches = ['sortOrder=asc', 'sortOrder=desc'];

...to get the index of the first match, you can use findIndex()

const array = ["page=4", "sortOrder=asc", "datePosted=all-time", "sortOrder=desc"];
const index = array.findIndex(value => /^sortOrder=/.test(value));
// index = 1;

If you are not using ES6, here's the code in ES5.

like image 118
pmrotule Avatar answered Oct 17 '22 19:10

pmrotule


Sadly there's no partial match in indexOf... Try this and see if it helps:

Array.prototype.MatchInArray = function (value) {

    var i;

    for (i=0; i < this.length; i++) {

        if (this[i].match(value)) {

           return i;

       }

   }

   return -1;

};
like image 30
Yëco Avatar answered Oct 17 '22 21:10

Yëco


If you want to find an item in an array, you'll have to iterate over it. If you array is in a known order (eg: it is sorted alphabetically), then there are some efficiencies you could build in to the search algorithm (eg: binary tree search), however unless you have thousands of items in the array it's hardly going to be worth it. In your case, just loop over the array checking each item against your regex and return when you find a match.

like image 1
nickf Avatar answered Oct 17 '22 19:10

nickf


New Array.findIndex function from ES6 makes it possible to do what you want, use regex and not iterate over array.

Here you have a sample of how it can be done: https://jsbin.com/qemeseyeme/edit?js,console

Full code sample:

const arr = ["page=4", "sortOrder=asc", "datePosted=all-time"];
const str = "sortOrder";

function isStringInArray(str, arr) {

  if (arr.findIndex(_strCheck) === -1) return false;

  return true;

  function _strCheck(el) {

    return el.match(str);
  }
}

console.log(isStringInArray(str, arr));
like image 1
Lukasz Gornicki Avatar answered Oct 17 '22 19:10

Lukasz Gornicki