admin管理员组

文章数量:1321817

Currently I have implemented a Javascript Ajax search where if user types a city name I am populating the name in the search result. Problem is there are cities which have alternate names (for example Mumbai has Bombay as alternate name.....Bangalore has Bengaluru as alternate name). I want to map the alternate names of the city to the correct name using javascript. ALSO there might be multiple alternate names for the city not only one. so how can I map alternate cities name to correct one using javascript.

Currently I have implemented a Javascript Ajax search where if user types a city name I am populating the name in the search result. Problem is there are cities which have alternate names (for example Mumbai has Bombay as alternate name.....Bangalore has Bengaluru as alternate name). I want to map the alternate names of the city to the correct name using javascript. ALSO there might be multiple alternate names for the city not only one. so how can I map alternate cities name to correct one using javascript.

Share Improve this question edited Jun 16, 2012 at 2:17 ACarter 5,7079 gold badges42 silver badges59 bronze badges asked Jun 15, 2012 at 16:28 KalishKalish 8332 gold badges9 silver badges18 bronze badges 5
  • You could probably do a regex search... Or put the possible names into an array and pare against it? – ayyp Commented Jun 15, 2012 at 16:31
  • var alternateCityNames = {Bangalore:bangaluru, Mumbai:bombay} I am able to get the user entered string and able to pare against my array of alternate cities name, but I am not sure how do I replace the search string with the actual city string....I am sorry but I am not very clear about array – Kalish Commented Jun 15, 2012 at 16:35
  • Show how do you obtain this string. Reverting to put it back will be pretty simple (though it have absolutely nothing to do with arrays/objects or string mapping at all). – Oleg V. Volkov Commented Jun 15, 2012 at 16:37
  • @Kalish, in my example, the user input is stored in var city, in Oleg's, it's stored in var userCity, follow either answer and it will work. My answer shows a cool example of defaulting values using the || operator – Ruan Mendes Commented Jun 15, 2012 at 16:42
  • thanks for suggestions, I will implement and let you all know...this forum is really worth exploring...so quick reply...thanks everryone! – Kalish Commented Jun 15, 2012 at 16:47
Add a ment  | 

2 Answers 2

Reset to default 6
var duplicateNameCities = {
    "Bengaluru": "Bangalore",
    "Mumbai": "Bombay"
};

// when you're given a string, run it through this map first
city = duplicateNameCities[city] || city;

However, it feels like the server side should handle this

var alternates = {
    Bombay    : "Mumbai",
    Bengaluru : "Bangalore",
}

var userCity = obtainUserInputSomehow()
if (alternates.hasOwnProperty(userCity)) { userCity = alternates[userCity] }
// after this userCity will have "Mumbai" if user entered "Bombay" or "Mumbai".
// add as many aliases as you need.

本文标签: jqueryString Mapping in JavascriptStack Overflow