简体   繁体   English

将值与数组进行比较

[英]Comparing a value against an array

I am attempting to compare value against my array, however, it is not working as it should. 我正在尝试将值与我的数组进行比较,但是,它无法正常工作。 When the value is Ontario it should be returned to be abbreviated as 'ON' and when the value is not found in the array list then it should be returned the same value as it was put it. 当值为Ontario时,应将其返回缩写为“ ON”,而在数组列表中未找到该值时,应返回与输入时相同的值。 The first alert box returns 'Ontario'? 第一个警报框返回“安大略省”? What am I doing wrong here: 我在这里做错了什么:

function shorten(value) {

    var prov_states = [
        ['alberta','AB'],
        ['british Columbia','BC'],
        ['manitova','MB'],
        ['new brunswick','NB'],
        ['newfoundland and labrador','NL'],
        ['nova scotia','NS'],
        ['northwest territories','NT'],
        ['new brunswick','NB'],
        ['nunavut','NU'],
        ['ontario','ON'],
        ['prince edward island','PE'],
        ['quebec','QC'],
        ['saskatchewan','SK'],
        ['yukon','YT'],
        ['alabama','AL'],    
        ['alaska','AK'],
        ['arizona','AZ'],
        ['arkansas','AR'],
        ['california','CA'],
        ['colorado','CO'],
        ['connecticut','CT'],
        ['delaware','DE'],
        ['florida','FL'],
        ['georgia','GA'],
        ['hawaii','HI'],
        ['idaho','ID'],
        ['illinois','IL'],
        ['indiana','IN'],
        ['iowa','IA'],
        ['kansas','KS'],
        ['kentucky','KY'],
        ['louisiana','LA'],
        ['maine','ME'],
        ['maryland','MD'],
        ['massachusetts','MA'],
        ['michigan','MI'],
        ['minnesota','MN'],
        ['mississippi','MS'],       
        ['missouri','MO'],
        ['montana','MT'],    
        ['nebraska','NE'],
        ['nevada','NV'],
        ['new hampshire','NH'],
        ['new jersey','NJ'],
        ['new mexico','NM'],
        ['new york','NY'],
        ['north carolina','NC'],
        ['north dakota','ND'],
        ['ohio','OH'],
        ['oklahoma','OK'],
        ['oregon','OR'],
        ['pennsylvania','PA'],
        ['rhode island','RI'],
        ['south carolina','SC'],
        ['south dakota','SD'],
        ['tennessee','TN'],
        ['texas','TX'],
        ['utah','UT'],
        ['vermont','VT'],
        ['virginia','VA'],      
        ['washington','WA'],
        ['west virginia','WV'],
        ['wisconsin','WI'],
        ['wyoming','WY']            
        ]


  for (var i=0, iLen=prov_states.length; i<iLen; i++) {

    if (value.toLowerCase() == prov_states[i][0]) { return prov_states[i][1]; }

    else { return value }

  }
}


function test() {

alert(shorten('Ontario'))

alert(shorten('DC'))


}

You probably want to use a map/associative array instead of what you have now; 您可能想使用一个映射/关联数组,而不是现在的数组。 it's much easier to do it this way. 这样做起来容易得多。

var prov_states = {
    "alberta": "AB",
    "british columbia": "BC",
    ...
}

Then you can just do: 然后,您可以执行以下操作:

return prov_states[value.toLowerCase()];

Your particular problem is because of what you're doing when you don't find a value: 您的特定问题是因为找不到价值时您正在做的事情:

else { return value }

This means that if the first match is not successful, you immediately return value . 这意味着,如果第一个匹配失败,则立即返回value Instead you should only do that if you were unable to find a match at all. 相反,只有在完全找不到匹配项时才应该这样做。

A while loop should work: while循环应该起作用:

var i = 0;
var state = null;
while(state === null && i < prov_states.length) {
    if(value.toLowerCase() === prov_states[i][0]) {
        state = prov_states[i][1];
    }

    i++;
}

return state === null ? value : state; //or just return state if you are okay with null

Another issue I see is that you have BC as "british Columbia" , which will never match because you are calling value.toLowerCase() , which lowercases the entire string. 我看到的另一个问题是,您将BC称为"british Columbia" ,这将永远不会匹配,因为您正在调用value.toLowerCase() ,这会小写整个字符串。

your problem is with this 你的问题是这个

else { return value }

It's going to return the first time it checks a state and it doesn't match. 它会在第一次检查状态且不匹配时返回。

The simplest change you can do it the following 您可以执行以下最简单的更改

for (var i=0, iLen=prov_states.length; i<iLen; i++) {
    if (value.toLowerCase() == prov_states[i][0]) { 
        return prov_states[i][1]; 
    }    
}
return null;//or whatever you want to return when it is not found.

An even better implementation would be this I know that I was ninja'd by Vivin Paliath on this one 一个更好的实现是, 我知道我是Vivin Paliath在这个游戏上的忍者

var prov_states = {
    'alberta': 'AB',
    'british Columbia': 'BC'
};

...

alert(prov_states['Ontario'.toLowerCase()]);

Put the return statement of else outside of the loop. 将else的return语句放在循环之外。 Remove the else. 删除其他。

for (var i=0, iLen=prov_states.length; i<iLen; i++) {
  if (value.toLowerCase() == prov_states[i][0]) return prov_states[i][1];
}
return value;

Move the return value statement out of the for loop (and the else ). return value语句移出for循环(和else )。 Your code is currently returning the supplied value if the first item in the loop doesn't match. 如果循环中的第一项不匹配,则您的代码当前正在返回提供的值。

for (var i=0, iLen=prov_states.length; i<iLen; i++)
{
    if (value.toLowerCase() == prov_states[i][0])
    {
        return prov_states[i][1];
    }
}
return value;

The first value of your array is not equal to Ontario . 数组的第一个值不等于Ontario Therefore, the first time your loop runs, your else condition is called, returning value . 因此,第一次运行循环时,将调用else条件,并返回value You should make the loop look like this: 您应该使循环看起来像这样:

for (var i=0, iLen=prov_states.length; i<iLen; i++) {
   if (value.toLowerCase() == prov_states[i][0]) { return prov_states[i][1]; }
}

return value;

Put your return statement AFTER the whole loop. 将您的return语句放在整个循环之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM