javascript indexOf - 列表/数组中的完全匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27010306/
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
javascript indexOf - exact match in a list / array
提问by user1261774
I decided to use javascript indexOf to determine if a value exists in a list / array. This was working for me untill I realised that I need to find an exactmatch in the list.
我决定使用 javascript indexOf 来确定列表/数组中是否存在一个值。这对我有用,直到我意识到我需要在列表中找到完全匹配。
Here is my example code:
这是我的示例代码:
if (['eu', 'fr-CA', 'lt', 'sv', 'zh-CN', 'zh-TW'].indexOf('fr') > -1) {
The above code will 'match' fr to fr-CA, which is a problem for me, as I must have an exact match to frand not match to fr-CA.
上面的代码将 fr 与 fr-CA “匹配”,这对我来说是一个问题,因为我必须与 fr 完全匹配,而与 fr-CA 不匹配。
I thought that this would be straight forward, but I have searched SO and google, and cannot find an answer to getting the exact match.
我认为这会很简单,但我已经搜索了 SO 和 google,但找不到获得完全匹配的答案。
回答by CD..
Thats wrong, you can use indexOf
:
那是错误的,您可以使用indexOf
:
arr.indexOf(searchElement[, fromIndex = 0])
indexOf
comparessearchElement
to elements of theArray
using strict equality (the same method used by the===
, or triple-equals, operator)
arr.indexOf(searchElement[, fromIndex = 0])
indexOf
与using 严格相等的searchElement
元素进行比较Array
(与===
或三重相等运算符使用的方法相同)
So ['eu', 'fr-CA', 'lt', 'sv', 'zh-CN', 'zh-TW'].indexOf('fr')
returns -1
.
所以['eu', 'fr-CA', 'lt', 'sv', 'zh-CN', 'zh-TW'].indexOf('fr')
返回-1
。
回答by filur
Try this:
试试这个:
var match = ['eu', 'fr-CA', 'lt', 'sv', 'zh-CN', 'zh-TW'].filter(function(item){
return item === 'eu';
});
console.log(match); // an array of matches, only one item in this case
Though your could should work fine. Perhaps you missed something else?
虽然你可以正常工作。也许你错过了别的东西?
回答by Sudharsan S
indexOf
when compare array value is equal to the same value in the array. then only it returns true otherwise it always returns false
indexOf
当比较数组值等于数组中的相同值时。那么只有它返回真否则它总是返回假
if you want to be search substring in the array value use this below function. it search the substring in the array and returns the position in the array
如果您想在数组值中搜索子字符串,请使用以下函数。它搜索数组中的子字符串并返回数组中的位置
function searchStringInArray (str, strArray) {
for (var j=0; j<strArray.length; j++) {
if (strArray[j].match(str)) return j;
}
return -1;
}
var res = searchStringInArray('fr',['eu', 'fr-CA', 'lt', 'sv', 'zh-CN', 'zh-TW']);
alert(res);
回答by dfsq
You can use some
method. It will stop search once the first match is found, or return false if nothing matches:
您可以使用some
方法。一旦找到第一个匹配项,它将停止搜索,如果没有匹配项,则返回 false:
var arr = ['eu', 'fr-CA', 'lt', 'sv', 'zh-CN', 'zh-TW'];
var match = arr.some(function(el) {
return el === 'zh-CN';
});
alert(match);
Or regular expressions can also be used. For example:
或者也可以使用正则表达式。例如:
var arr = ['eu', 'fr-CA', 'lt', 'sv', 'zh-CN', 'zh-TW'];
var match = new RegExp('^' + arr.join('|') + '$').test('zh-CN');
alert(match);