我正在寻找一个简单的函数来检查我的数组中是否有特定的字符串。
function checkRegion(departement){
var region = '';
if(alsace.indexOf(departement) != -1)
{
region = "alsace";
}
if(aquitaine.indexOf(departement) != -1){
region = "aquitaine";
}
if(auvergne.indexOf(departement) != -1){
region = "auvergne";
}
if(basseNormandie.indexOf(departement) != -1){
region = "basse-normandie";
}
if(iledefrance.indexOf(departement) != -1){
region = "ile-de-france";
}else{
region = 'undennnnnfined';
}
return region;
};
任何解决方案?
由于
答案 0 :(得分:3)
您的问题在于使用连续if
语句而不将它们链接在一起以进行全面检查。
按照自己的方式行事,代码实际上完全忽略了所有if
语句,但最后一个。
因此,如果 iledefrance.indexOf(departement) != -1
提供 false
,那么它将始终执行 else
,意思是设置 region = 'undennnnnfined'
。
注意:强>
在代码中,我将 != -1
替换为 ~
,因为它提供了更简洁的代码。从本质上讲,它会将 -1
转换为 0
,即 false
。
如果您不熟悉,请务必查看MDN's documentation以获取更多信息。
• 1 st 选项:
尝试以 if
格式将 if/else if/else
语句链接在一起,如下所示:
function checkRegion(departement){
var region = '';
if(~alsace.indexOf(departement)) {
region = "alsace";
}
else if(~aquitaine.indexOf(departement)) {
region = "aquitaine";
}
else if(~auvergne.indexOf(departement)) {
region = "auvergne";
}
else if(~basseNormandie.indexOf(departement)) {
region = "basse-normandie";
}
else if(~iledefrance.indexOf(departement)) {
region = "ile-de-france";
}
else{
region = 'undennnnnfined';
}
return region;
};
• 2 nd 选项:
创建两个数组:
alsace
, aquitaine
等)&amp; <强>代码:强>
function checkRegion(departement) {
var
regions = [alsace, aquitaine, auvergne, basseNormandie, iledefrance],
regionsNames = ["alsace", "aquitaine", "auvergne", "basseNormandie", "iledefrance"];
for (var i = 0; i < regions.length; i++) {
if (~regions[i].indexOf(departement)) {
return regionsNames[i];
}
}
return "undennnnnfined"; // If the 'for' loop doesn't return anything, it's false.
};
<强>演示:强>
<强>片断:强>
function checkRegion(departement){
var region = '';
if(~alsace.indexOf(departement)) {
region = "alsace";
}
else if(~aquitaine.indexOf(departement)) {
region = "aquitaine";
}
else if(~auvergne.indexOf(departement)) {
region = "auvergne";
}
else if(~basseNormandie.indexOf(departement)) {
region = "basse-normandie";
}
else if(~iledefrance.indexOf(departement)) {
region = "ile-de-france";
}
else{
region = 'undennnnnfined';
}
return region;
};
var
alsace = ["Strasbourg", "Colmar"],
aquitaine = ["Gironde", "Landes", "Dordogne", "Pyrenees-Atlantiques", "Lot-et-Garonne"],
auvergne = [],
basseNormandie = [],
iledefrance = [];
alert(checkRegion("Strasbourg"));
function checkRegion(departement) {
var
regions = [alsace, aquitaine, auvergne, basseNormandie, iledefrance],
regionsNames = ["alsace", "aquitaine", "auvergne", "basseNormandie", "iledefrance"];
for (var i = 0; i < regions.length; i++) {
if (~regions[i].indexOf(departement)) {
return regionsNames[i];
}
}
return "undennnnnfined"; // If the 'for' loop doesn't return anything, it's false.
};
var
alsace = ["Strasbourg", "Colmar"],
aquitaine = ["Gironde", "Landes", "Dordogne", "Pyrenees-Atlantiques", "Lot-et-Garonne"],
auvergne = [],
basseNormandie = [],
iledefrance = [];
alert(checkRegion("Strasbourg"));