2017-02-01 13 views
0

母国語の名前をフィルターにかけ、母音で始まる国を返すjavascript関数を作成しようとすると、非常に困難です。国母音フィルター機能

これは私がこれまで持っているものです。

var countries = ["Lithuania", "Congo", "Micronesia, Federated States of", "Gabon", "United Arab Emirates", "Turkey", "Northern Mariana Islands", "Hungary", "France Metropolitan", "Virgin Islands (U.S.)", "Singapore", "Falkland Islands (Malvinas)", "Cyprus", "Rwanda", "Somalia", "Chile", "Libyan Arab Jamahiriya", "Uzbekistan", "Ghana", "Bahamas", "Austria", "Monaco", "Iceland", "Tajikistan", "Estonia", "France", "Central African Republic", "Martinique", "Mongolia", "Brunei Darussalam", "French Guiana", "Cape Verde", "Comoros", "Macedonia, The Former Yugoslav Republic of", "Antigua and Barbuda", "United States Minor Outlying Islands", "Bouvet Island", "Maldives", "Cocos (Keeling) Islands", "South Georgia and the South Sandwich Islands", "Jordan", "Bhutan", "Finland", "Luxembourg", "Vanuatu", "Uganda", "Thailand", "Liberia", "Ethiopia", "Suriname", "Korea, Democratic People's Republic of", "Niger", "Burkina Faso", "Italy", "Indonesia", "Nepal", "Mauritania", "Cameroon", "Fiji", "Solomon Islands", "Ireland", "Philippines", "Tuvalu", "Sudan", "Guadeloupe", "Venezuela", "Trinidad and Tobago", "Samoa", "Niue", "Papua New Guinea", "Hong Kong", "Burundi", "Virgin Islands (British)", "Uruguay", "Vietnam", "Oman", "Christmas Island", "Israel", "Montserrat", "Czech Republic", "Senegal", "Afghanistan", "Greenland", "Norway", "Eritrea", "Haiti", "Tunisia", "Swaziland", "Guinea-Bissau", "Botswana", "Greece", "Slovakia (Slovak Republic)", "Netherlands", "Jamaica", "St. Pierre and Miquelon", "Ecuador", "Morocco", "Grenada", "Poland", "Malaysia", "Mauritius", "Gambia", "Zimbabwe", "Bosnia and Herzegowina", "Malawi", "Puerto Rico", "United States", "Syrian Arab Republic", "Azerbaijan", "Cook Islands", "Cayman Islands", "Svalbard and Jan Mayen Islands", "Korea, Republic of", "Costa Rica", "Dominica", "Slovenia", "Australia", "Peru", "Albania", "Palau", "Netherlands Antilles", "Saudi Arabia", "Canada", "Moldova, Republic of", "Faroe Islands", "Heard and Mc Donald Islands", "Croatia (Hrvatska)", "Andorra", "Nicaragua", "Panama", "Tokelau", "South Africa", "Lesotho", "French Southern Territories", "Algeria", "Guinea", "Aruba", "Saint Lucia", "Togo", "Turks and Caicos Islands", "Yugoslavia", "Liechtenstein", "Latvia", "Kyrgyzstan", "Cuba", "Zambia", "Marshall Islands", "Mozambique", "Sierra Leon"]; 

function CountryFilter(input) { 
    for (i=0; i<=input.length; i++) { 
    if (input.charAt(0) === "a","e","i","o","u") { 
     return input; 
    } 
    } 
} 

document.write(CountryFilter(countries)); 

答えて

0
var startsWithVowels = countries.filter(function(country) { 
    var firstLetter = country.charAt(0); 
    return firstLetter.match(/[aeiou]/i); // or firstLetter.match([AEIOU]) 
}); 

console.log(startsWithVowels); 

/ - 文字がaまたはeまたはiまたはoかであればマッチ - 正規表現

[aeiou]の開始をマークu

/ - 正規表現の末尾に表示

i - 大文字小文字を区別しないようにするフラグ。

すべての国の最初の文字が大文字になりますが、遭遇する可能性のあるデータが分かっているため、iフラグは必要ありません。 )。データについての仮定をしないように正規表現の詳細情報については、コードhere

プレイ優れている、確かに仕事をしないことにドキュメントhere

+0

を読んで、しかし、あなたは説明できそれは少しですか? OPはおそらく '.filter()'とRegExpについて知りません。 – yuriy636

+0

@ yuriy636答えを更新しました。 –

+0

ありがとう!私はちょうどこのサイトに参加しました。私はどのくらい速く私が助けを受けることができたかに驚いています! – csagar