Javascript 如何在字符串数组中搜索字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5424488/
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
How to search for a string inside an array of strings
提问by someuser
After searching for an answer in other posts, I felt I have to ask this. I looked at How do I check if an array includes an object in JavaScript?and Best way to find if an item is in a JavaScript array?and couldn't get the code there to work.
在其他帖子中寻找答案后,我觉得我必须问这个。我查看了如何检查数组是否包含 JavaScript 中的对象?以及查找项目是否在 JavaScript 数组中的最佳方法?并且无法让代码在那里工作。
I am capturing an html embed code into an array, so each item is a line of html code.
我正在捕获一个 html 嵌入代码到一个数组中,所以每个项目都是一行 html 代码。
for example:
例如:
i[0]='<param name=\"bgcolor\" value=\"#FFFFFF\" />'
i[1]='<param name=\"width" value=\"640\" />'
i[2]='<param name=\"height\" value=\"360\" />'
i[3]='more html code'
I want to search this array and find the line where the word 'height' is.
我想搜索这个数组并找到“高度”这个词所在的行。
So ideally, I'd like to write a function that returns the index of the item where 'height' appears.
所以理想情况下,我想编写一个函数来返回出现“高度”的项目的索引。
Fortunately, there are no duplicates in this array, so there's only one occurrence.
幸运的是,此数组中没有重复项,因此只有一次出现。
with simple object search I get 'false' returns.
通过简单的对象搜索,我得到 'false' 回报。
Any idea?
任何的想法?
回答by Aleadam
It's as simple as iterating the array and looking for the regexp
它就像迭代数组并寻找正则表达式一样简单
function searchStringInArray (str, strArray) {
for (var j=0; j<strArray.length; j++) {
if (strArray[j].match(str)) return j;
}
return -1;
}
Edit- make str
as an argument to function.
编辑-str
作为函数的参数。
回答by Janick Bernet
Extending the contains function you linked to:
扩展您链接到的 contains 功能:
containsRegex(a, regex){
for(var i = 0; i < a.length; i++) {
if(a[i].search(regex) > -1){
return i;
}
}
return -1;
}
Then you call the function with an array of strings and a regex, in your case to look for height:
然后你用一个字符串数组和一个正则表达式调用函数,在你的情况下寻找高度:
containsRegex([ '<param name=\"bgcolor\" value=\"#FFFFFF\" />', 'sdafkdf' ], /height/)
You could additionally also return the index where height was found:
您还可以返回找到高度的索引:
containsRegex(a, regex){
for(var i = 0; i < a.length; i++) {
int pos = a[i].search(regex);
if(pos > -1){
return [i, pos];
}
}
return null;
}
回答by Chetan Khilosiya
You can use Array.prototype.find function in javascript. Array find MDN.
您可以在 javascript 中使用 Array.prototype.find 函数。 数组查找 MDN。
So to find string in array of string, the code becomes very simple. Plus as browser implementation, it will provide good performance.
所以要在字符串数组中查找字符串,代码就变得很简单了。加上作为浏览器实现,它将提供良好的性能。
Ex.
前任。
var strs = ['abc', 'def', 'ghi', 'jkl', 'mno'];
var value = 'abc';
strs.find(
function(str) {
return str == value;
}
);
or using lambda expression it will become much shorter
或者使用 lambda 表达式它会变得更短
var strs = ['abc', 'def', 'ghi', 'jkl', 'mno'];
var value = 'abc';
strs.find((str) => str === value);
回答by Alex W
It's faster to avoid using regular expressions, if you're just trying to find the first substring match within an array of string values. You can add your own array searching function:
如果您只是想在字符串值数组中查找第一个子字符串匹配,则避免使用正则表达式会更快。您可以添加自己的数组搜索功能:
Code:
代码:
Array.prototype.findFirstSubstring = function(s) {
for(var i = 0; i < this.length;i++)
{
if(this[i].indexOf(s) !== -1)
return i;
}
return -1;
};
Usage:
用法:
i.findFirstSubstring('height');
Returns:
回报:
-1 if not found or the array index of the first substring occurrence if it is found (in your case would be 2
)
-1 如果未找到,或者找到第一个子字符串出现的数组索引(在您的情况下为2
)