2015-11-23 102 views
0

嗨,我需要將文本輸入和數組轉換爲小寫,所以輸入時它可以匹配數組。我不斷收到錯誤,表示airline.Array.contains不是一個函數。任何人都可以幫忙嗎?隱藏輸入爲小寫

var euAirlines = [ 
     "British Airways", 
     "Austrian Airlines", 
     "Blue Islands", 
     "KLM", 
     "TAP Portugal", 
     "Air Malta", 
     "Air France", 
     "germanwings", 
     "Wideroe", 
     "Lufthansa", 
     "Aegean Airlines", 
     "Citywing", 
     "Flybe", 
     "Aurigny", 
     "airBaltic", 
     "Swiss", 
     "eurowings", 
     "Croatia Airlines", 
     "SAS", 
     "LOT", 
     "Air Serbia", 
     "Iberia", 
     "Wizz Air", 
     "Blue Air", 
     "easyJet", 
     "Air Europa", 
     "Czech Airlines", 
     "Aer Lingus", 
     "Eastern Airways", 
     "Smart Wings", 
     "Aeroflot", 
     "Thomson Airways", 
     "bmi regional", 
     "Ryanair", 
     "Monarch", 
     "TAROM", 
     "Brussels Airlines", 
     "Jet2", 
     "Meridiana", 
     "Vueling Airlines", 
     "CityJet", 
     "Scilly Skybus", 
     "Alitalia", 
     "Thomas Cook Airlines", 
     "Garuda Indonesia", 
     "Norwegian", 
     "Ukraine International", 
     "Bulgaria Air", 
     "Finnair", 
     "Luxair" 
    ]; 

    Array.prototype.contains = function (needle) { 
     for (i in this) { 
      if (this[i] == needle) return true; 
     } 
     return false; 
    } 

    var airlineInput = document.getElementById("airlineInput"), 
     airlineArray = euAirlines.toString().toLowerCase(); 
    document.getElementById("claim-submit").onclick = function(e) { 
     var lower = airlineInput.value.toLowerCase(); 
     console.log(lower); 
     if (airlineArray.contains(lower.value)) { 

     console.log("Match"); 
     }else { 

     console.log("No Match") 
     } 

    } 
+1

這是因爲它不是一個數組...'airlineArray = euAirlines.toString()toLowerCase();'不做你的想法 –

+0

airlineArray我想所有的數組值都是小寫,然後檢查文本輸入。 – Doddsy

+0

在'toLowerCase()'後添加'.split(',')''''''' –

回答

0

您不需要編寫包含數組作爲字符串已經有一個包含功能。你可以像這樣使用。

var lowerCaseArrayString = euAirlines.toString().toLowerCase(); 
if(lowerCaseArrayString.includes(lower)){ 
    console.log("matched"); 
}else{ 
    console.log("not matched"); 
} 

或者如果你還想用數組,那麼你可以使用這個。

var lowerCaseArray = euAirlines.toString().toLowerCase().split(","); 
if(lowerCaseArray.indexOf(lower) > -1){ 
    console.log("matched"); 
}else{ 
    console.log("not matched"); 
} 
+0

它不是'contains',而是['includes'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) – R3tep

+0

@ R3tep好的,我檢查了。 'Includes'好得多,因爲它支持所有的瀏覽器,但Firefox仍然有包含'includes'的字符串的'contains'方法。 –

1

您可以輕鬆創建一個降低航空公司這樣的新列表。

var lowercaseEUAirlines = euAirlines.map(function(airline) { 
    return airline.toLowerCase(); 
}); 

然後,您可以輕鬆地在它使用foreach循環:

lowercaseEUAirlines.forEach(function(element, index, array) { 
    console.log(element); 
}); 
0

可能這裏是你搜索的內容:

var euAirlines = [ 
 
     "British Airways", 
 
     "Austrian Airlines", 
 
     "Blue Islands", 
 
     "KLM", 
 
     "TAP Portugal", 
 
     "Air Malta", 
 
     "Air France", 
 
     "germanwings", 
 
     "Wideroe", 
 
     "Lufthansa", 
 
     "Aegean Airlines", 
 
     "Citywing", 
 
     "Flybe", 
 
     "Aurigny", 
 
     "airBaltic", 
 
     "Swiss", 
 
     "eurowings", 
 
     "Croatia Airlines", 
 
     "SAS", 
 
     "LOT", 
 
     "Air Serbia", 
 
     "Iberia", 
 
     "Wizz Air", 
 
     "Blue Air", 
 
     "easyJet", 
 
     "Air Europa", 
 
     "Czech Airlines", 
 
     "Aer Lingus", 
 
     "Eastern Airways", 
 
     "Smart Wings", 
 
     "Aeroflot", 
 
     "Thomson Airways", 
 
     "bmi regional", 
 
     "Ryanair", 
 
     "Monarch", 
 
     "TAROM", 
 
     "Brussels Airlines", 
 
     "Jet2", 
 
     "Meridiana", 
 
     "Vueling Airlines", 
 
     "CityJet", 
 
     "Scilly Skybus", 
 
     "Alitalia", 
 
     "Thomas Cook Airlines", 
 
     "Garuda Indonesia", 
 
     "Norwegian", 
 
     "Ukraine International", 
 
     "Bulgaria Air", 
 
     "Finnair", 
 
     "Luxair" 
 
    ]; 
 

 
var checking = function (e, val) { 
 
    if (e.keyCode == 13) { 
 
    $('body').append($('<p>').text($.inArray(val, euAirlines) > -1 ? 'Match' : 'Not match')) 
 
    } 
 
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" id="airlineInput" onkeyup="checking(event, $(this).val().toLowerCase())" />

爲了測試我的榜樣:輸入GermanwingsEurowings並按回車進行檢查。

0

您已將數組轉換爲字符串(airlineArray = euAirlines.toString().toLowerCase()),並且字符串沒有屬性contains

您似乎想要檢查航空公司陣列中輸入是否存在(部分)匹配。如果是這樣的話,你可以使用Array.prototype.filter找到匹配的元素,並String.prototype.indexOf找到,如果它匹配,就像這樣:

var euAirlines = [ 
    "British Airways", 
    "Austrian Airlines", 
    "Blue Islands", 
    "KLM", 
    "TAP Portugal", 
    "Air Malta", 
    "Air France", 
    "germanwings", 
    "Wideroe", 
    "Lufthansa", 
    "Aegean Airlines", 
    "Citywing", 
    "Flybe", 
    "Aurigny", 
    "airBaltic", 
    "Swiss", 
    "eurowings", 
    "Croatia Airlines", 
    "SAS", 
    "LOT", 
    "Air Serbia", 
    "Iberia", 
    "Wizz Air", 
    "Blue Air", 
    "easyJet", 
    "Air Europa", 
    "Czech Airlines", 
    "Aer Lingus", 
    "Eastern Airways", 
    "Smart Wings", 
    "Aeroflot", 
    "Thomson Airways", 
    "bmi regional", 
    "Ryanair", 
    "Monarch", 
    "TAROM", 
    "Brussels Airlines", 
    "Jet2", 
    "Meridiana", 
    "Vueling Airlines", 
    "CityJet", 
    "Scilly Skybus", 
    "Alitalia", 
    "Thomas Cook Airlines", 
    "Garuda Indonesia", 
    "Norwegian", 
    "Ukraine International", 
    "Bulgaria Air", 
    "Finnair", 
    "Luxair" 
]; 

function containsLowerCaseMatch(needle, value) { 
    return value.toLowerCase().indexOf(needle.toLowerCase()) > -1; 
} 

var airlineInput = document.getElementById("airlineInput"); 
var matches = euAirlines.filter(containsLowerCaseMatch.bind(null, airlineInput.value)); 

if (matches.length > 0) { 
    console.log('Match'); 
} else { 
    console.log('No match'); 
} 

的代碼還使用Function.prototype.bind通過輸入的文本。

與自動完成完整的示例

var euAirlines = [ 
 
    "British Airways", 
 
    "Austrian Airlines", 
 
    "Blue Islands", 
 
    "KLM", 
 
    "TAP Portugal", 
 
    "Air Malta", 
 
    "Air France", 
 
    "germanwings", 
 
    "Wideroe", 
 
    "Lufthansa", 
 
    "Aegean Airlines", 
 
    "Citywing", 
 
    "Flybe", 
 
    "Aurigny", 
 
    "airBaltic", 
 
    "Swiss", 
 
    "eurowings", 
 
    "Croatia Airlines", 
 
    "SAS", 
 
    "LOT", 
 
    "Air Serbia", 
 
    "Iberia", 
 
    "Wizz Air", 
 
    "Blue Air", 
 
    "easyJet", 
 
    "Air Europa", 
 
    "Czech Airlines", 
 
    "Aer Lingus", 
 
    "Eastern Airways", 
 
    "Smart Wings", 
 
    "Aeroflot", 
 
    "Thomson Airways", 
 
    "bmi regional", 
 
    "Ryanair", 
 
    "Monarch", 
 
    "TAROM", 
 
    "Brussels Airlines", 
 
    "Jet2", 
 
    "Meridiana", 
 
    "Vueling Airlines", 
 
    "CityJet", 
 
    "Scilly Skybus", 
 
    "Alitalia", 
 
    "Thomas Cook Airlines", 
 
    "Garuda Indonesia", 
 
    "Norwegian", 
 
    "Ukraine International", 
 
    "Bulgaria Air", 
 
    "Finnair", 
 
    "Luxair" 
 
]; 
 

 
function containsLowerCaseMatch(needle, value) { 
 
    return value.toLowerCase().indexOf(needle.toLowerCase()) > -1; 
 
} 
 

 
var airlineInput = document.getElementById("airlineInput"), suggestions = document.getElementById("suggestions"); 
 

 
airlineInput.onkeyup = function() { 
 
    suggestions.innerHTML = ''; 
 

 
    var matches = euAirlines.filter(containsLowerCaseMatch.bind(null, airlineInput.value)).forEach(function(value) { 
 
    var li = document.createElement('li'); 
 
    li.appendChild(document.createTextNode(value)); 
 
    suggestions.appendChild(li); 
 
    }); 
 
}; 
 

 
airlineInput.onkeyup();
<input id=airlineInput name=airlineInput> 
 
<h1>Suggestions</h1> 
 
<ol id=suggestions></ol>

你顯然需要以使其適應你的代碼。

+0

如果使用現代瀏覽器,它確實有'includes()':https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes –

+0

@NeilMunro我知道,但我提供的代碼在IE9 +中受支持。此外,它還允許您提供自動完成功能(我想像OP計劃執行的功能),而不會發生任何重大更改或重複。 – Toothbrush

0

只需使用映射:

newArray = map(function(s){s.toLowerCase()}, oldArray) 
+0

這是[** @ NeilMunro **的答案](https://stackoverflow.com/a/33871105/3210837)的副本。 – Toothbrush

+0

我假設你的意思是寫'newArray = oldArray.map(function(s){s.toLowerCase()})'。 – Toothbrush

0
var euAirlines = [ 
     "British Airways", 
     "Austrian Airlines", 
     "Blue Islands", 
     "KLM", 
     "TAP Portugal", 
     "Air Malta", 
     "Air France", 
     "germanwings", 
     "Wideroe", 
     "Lufthansa", 
     "Aegean Airlines", 
     "Citywing", 
     "Flybe", 
     "Aurigny", 
     "airBaltic", 
     "Swiss", 
     "eurowings", 
     "Croatia Airlines", 
     "SAS", 
     "LOT", 
     "Air Serbia", 
     "Iberia", 
     "Wizz Air", 
     "Blue Air", 
     "easyJet", 
     "Air Europa", 
     "Czech Airlines", 
     "Aer Lingus", 
     "Eastern Airways", 
     "Smart Wings", 
     "Aeroflot", 
     "Thomson Airways", 
     "bmi regional", 
     "Ryanair", 
     "Monarch", 
     "TAROM", 
     "Brussels Airlines", 
     "Jet2", 
     "Meridiana", 
     "Vueling Airlines", 
     "CityJet", 
     "Scilly Skybus", 
     "Alitalia", 
     "Thomas Cook Airlines", 
     "Garuda Indonesia", 
     "Norwegian", 
     "Ukraine International", 
     "Bulgaria Air", 
     "Finnair", 
     "Luxair" 
    ]; 
    var new_euAirlines_lowercase=euAirlines.toString().toLowerCase().split(','); 
    Array.prototype.contains=function(input_value){ 
    new_euAirlines_lowercase.forEach(function(value,index){ 
     if(value == input_value) 
     console.log('matched') 
     else 
     console.log('did not match') 
    }); 
    } 

    $('#claim-submit').on('click',function(e){ 
     e.preventDefault(); 
     var input= $("#airlineInput").val().toLowerCase(); 
     Array.prototype.contains(input); 
    }); 

的jsfiddle鏈接:https://jsfiddle.net/Dee0565/gnnf86jh/