开发者

Find element in array using regex match without iterating over array

开发者 https://www.devze.com 2023-03-27 21:00 出处:网络
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

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.


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.


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;

};


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.


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));


Yes, you're wrong - you need to iterate. In ES5 there's a new method to find array positions - Array.indexOf but there's no support for this in older browsers and it needs an exact match not partial.

0

精彩评论

暂无评论...
验证码 换一张
取 消