如何在字符串中搜索多个值
How to search string for multiple values?
代码正常运行:
if ((filenameTmp == "thunderstorm") ||
(filenameTmp == "fog") ||
(filenameTmp == "hail") ||
(filenameTmp == "heavy_snow") ||
(filenameTmp == "rain") ||
(filenameTmp == "sleet") ||
(filenameTmp == "snow"))
{ document.getElementById("twilightBG").style.display = "none"; }
然而,我想缩短它,我认为这将工作,但它没有。
if(filenameTmp.indexOf ("thunderstorm", "fog", "hail", "heavy_snow", "haze", "sleet", "snow")> -1)
{ document.getElementById("twilightBG").style.display = "none"; }
如果我只有一个这样的搜索:
if(filenameTmp.indexOf ("haze")> -1)
{ document.getElementById("twilightBG").style.display = "none"; }
如何搜索多个实例?还是有更好的办法?谢谢。
三个选项:
-
地图对象
-
数组查找
-
switch
细节:
-
你可以使用一个查找映射对象:
// In a common declarations area var weather = { "thunderstorm": true, "fog": true, "hail": true, "heavy_snow": true, "rain": true, "sleet": true, "snow": true }; // Where you want the check if (weather[filenameTmp]) { document.getElementById("twilightBG").style.display = "none"; }
请注意,如果您愿意,可以内嵌:
if ({ "thunderstorm": true, "fog": true, "hail": true, "heavy_snow": true, "rain": true, "sleet": true, "snow": true }[filenameTmp]) { document.getElementById("twilightBG").style.display = "none"; }
请注意,如果
filenameTmp
具有"toString"
,"valueOf"
或类似的值,您将从中获得假阳性。如果你使用的是真正支持es5的引擎,你可以通过使用builder函数获得纯映射(没有toString
之类的对象):function pureMap(props) { var o = Object.create(null); var key; if (props) { for (key in props) { o[key] = props[key]; } } return o; }
:
// In a common declarations area var weather = pureMap({ "thunderstorm": true, "fog": true, "hail": true, "heavy_snow": true, "rain": true, "sleet": true, "snow": true }); // Where you want the check if (weather[filenameTmp]) { document.getElementById("twilightBG").style.display = "none"; }
-
或者您可以使用数组,但搜索是线性的,而浏览器可以优化上面的地图查找:
// In a common declarations area var weather = [ "thunderstorm", "fog", "hail", "heavy_snow", "rain", "sleet", "snow" ]; // Where you want the check if (weather.indexOf(filenameTmp) !== -1) { document.getElementById("twilightBG").style.display = "none"; }
同样,可以是inline:
if ([ "thunderstorm", "fog", "hail", "heavy_snow", "rain", "sleet", "snow" ].indexOf(filenameTmp) !== -1) { document.getElementById("twilightBG").style.display = "none"; }
-
还有
switch
选项:switch (filenameTmp) { case "thunderstorm": case "fog": case "hail": case "heavy_snow": case "rain": case "sleet": case "snow": document.getElementById("twilightBG").style.display = "none"; break; }
您可以将match()方法与正则表达式一起使用
var.match(^(?:apple|pear|whatever)$/)
您可以像这样使用数组方法:
if (["thunderstorm", "fog", "hail", "heavy_snow", "haze", "sleet", "snow"].indexOf(filenameTmp) >= 0) {
if (["thunderstorm", "fog", "hail", "heavy_snow", "haze", "sleet", "snow"].indexOf(filenameTmp) !== -1) {
document.getElementById("twilightBG").style.display = "none";
}
如果将contains方法添加到Array中,检查将更加清晰。直接:
Array.prototype.contains = function(obj) { return this.indexOf(obj) > -1; };
这允许检查为:
if (['thunderstorm', 'fog', 'hail', 'heavy_snow', 'rain', 'sleet', 'snow'].contains(filenameTmp)) {
document.getElementById("twilightBG").style.display = "none";
}
使用Array并遍历每个条目。这是最有效的方法,这样它就不会在遇到任何条目时循环遍历所有条目。
function containsAny(str, substrings) {
for (var i = 0; i != substrings.length; i++) {
var substring = substrings[i];
if (str == substring) {
return true;
}
}
return null;
}
var result = containsAny(filenameTmp, ["thunderstrom", "rain"]); // add values you want
if (result) {
document.getElementById("twilightBG").style.display = "none";
}
希望能有所帮助
对于出血边缘解决方案,请使用Set。请记住,Opera和Safari目前不支持:
var weather = new Set();
weather.add("thunderstorm");
weather.add("fog");
weather.add("hail");
weather.add("heavy_snow");
weather.add("rain");
weather.add("sleet");
weather.add("snow");
if (weather.has(filenameTmp)) {
document.getElementById("twilightBG").style.display = "none";
}
您可以使用regx:
if (filenameTmp.match(/^(thunderstorm|fog|hail|heavy_snow|rain|sleet|snow)$/)) {
document.getElementById("twilightBG").style.display = "none";
}
:
if (['thunderstorm', 'fog', 'hail', 'heavy_snow','rain','sleet','snow'].indexOf(filenameTmp) >= 0) {
document.getElementById("twilightBG").style.display = "none";
}
或Jquery的inArray方法:
if ($.inArray(filenameTmp, ['thunderstorm', 'fog', 'hail', 'heavy_snow','rain','sleet','snow']) >= 0) {
document.getElementById("twilightBG").style.display = "none";
}
相关文章:
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 在javascript中搜索字符串
- td:包含变量中的搜索字符串
- 在元素中搜索字符串,然后将其放入条件语句中
- Javascript正则表达式,用于验证接收到的MySQL全文搜索的搜索字符串
- 在对象数组中搜索字符串
- 在HTML中搜索字符串并返回True的Javascript条件
- 使用Tilde(~)和search()搜索字符串
- 在DOM中搜索字符串时不区分大小写
- 如何以编程方式输入搜索字符串并触发谷歌地图API的places_changed
- 在 Enter 上,在 db 中搜索字符串
- 使用引导表插件,如何在首次加载时设置搜索字符串
- 如何在 Firefox 25 中获取查找栏的最后一个搜索字符串
- JavaScript 搜索字符串包含
- 如何确定搜索字符串是否被较大字符串中的锚标记包装 - javascript 正则表达式
- 使用 forEach 在多个数组中搜索字符串
- 如何编写 angularjs 过滤器来搜索字符串片段/字符串序列
- Javascript:搜索字符串时数组中的多个表达式
- 如何使用 javascript 在
中搜索字符串,然后添加到该 id - 谷歌表格循环搜索字符串并清除相邻单元格