javascript 使用正则表达式匹配查找数组中的元素而不迭代数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7038575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:47:06  来源:igfitidea点击:

Find element in array using regex match without iterating over array

javascriptjqueryarrays

提问by endorphin

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.

使用 javascript 或 jquery 我想找到开始的元素的索引 "sortOrder="。我不会在编译时知道这个元素的完整字符串,只知道"sortOrder="部分。

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.

我假设这可以在不需要遍历数组和执行的情况下完成item.match("sortOrder="),但也许我错了。

Thanks for any help.

谢谢你的帮助。

回答by pmrotule

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

如果要获取匹配特定正则表达式的数组中的

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

...仅获取第一个匹配项的,您可以使用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 arrayof all match results, you can use filter()

...要获取所有匹配结果的数组,您可以使用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 indexof the first match, you can use findIndex()

...要获取第一个匹配项的索引,您可以使用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.

如果你没有使用 ES6,这里是ES5 中的代码

回答by Y?co

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

遗憾的是 indexOf 中没有部分匹配......试试这个,看看它是否有帮助:

Array.prototype.MatchInArray = function (value) {

 ? ?var i;

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

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

? ? ? ? ? ?return i;

? ? ? ?}

? ?}

? ?return -1;

};

回答by Lukasz Gornicki

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

来自 ES6 的新Array.findIndex函数可以做你想做的事,使用正则表达式而不是迭代数组。

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

这里有一个如何完成的示例: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));

回答by nickf

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.

如果你想在数组中找到一个项目,你必须遍历它。如果您的数组按已知顺序排列(例如:按字母顺序排序),那么您可以在搜索算法中构建一些效率(例如:二叉树搜索),但是除非数组中有数千个项目,否则很难将是值得的。在您的情况下,只需遍历数组,根据正则表达式检查每个项目,并在找到匹配项时返回。

回答by Andris

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

是的,你错了 - 你需要迭代。在 ES5 中,有一种查找数组位置的新方法 -Array.indexOf但旧浏览器不支持此方法,它需要精确匹配而不是部分匹配。