2

目前我已经实现了一个 Javascript Ajax 搜索,如果用户键入一个城市名称,我将在搜索结果中填充该名称。问题是有些城市有备用名称(例如,孟买有孟买作为备用名称......班加罗尔有班加罗尔作为备用名称)。我想使用 javascript 将城市的备用名称映射到正确的名称。此外,城市可能有多个替代名称,而不仅仅是一个。那么如何使用javascript映射备用城市名称以更正一个。

4

2 回答 2

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

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

但是,感觉服务器端应该处理这个

于 2012-06-15T16:31:52.933 回答
2
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.
于 2012-06-15T16:33:11.990 回答