Skip to content

Instantly share code, notes, and snippets.

@shenqihui
Last active August 29, 2015 14:16
Show Gist options
  • Save shenqihui/6342d58075e81a8f2b0e to your computer and use it in GitHub Desktop.
Save shenqihui/6342d58075e81a8f2b0e to your computer and use it in GitHub Desktop.
convertCity from geoip to chinese city
/**
* @name convertCity
* @desc 进行 geoip 到 中文 city 的转换。源变量污染式修改。
* @depend []
* @param { obj } obj 用于转换的数据
* @return { obj } 返回一个新的已经是中文名的 obj 。
**/
function convertCity(obj) {
var geoipcity = {
"Anhui Sheng": "安徽",
"Jilin Sheng": "吉林",
"Xizang": "西藏",
"Gansu Sheng": "甘肃",
"Inner Mongolia": "内蒙古",
"Chongqing Shi": "重庆",
"Hebei": "河北",
"Hainan": "海南",
"Ningxia Huizu Zizhiqu": "宁夏",
"Yunnan": "云南",
"Hubei": "湖北",
"Jiangxi Sheng": "江西",
"Beijing Shi": "北京市",
"Zhejiang Sheng": "浙江",
"Sichuan Sheng": "四川",
"Heilongjiang Sheng": "黑龙江",
"Shaanxi": "陕西",
"Xinjiang Uygur Zizhiqu": "新疆",
"Shanghai Shi": "上海",
"Fujian": "福建",
"Hunan": "湖南",
"Shandong Sheng": "山东",
"Jiangsu Sheng": "江苏",
"Qinghai Sheng": "青海",
"Shanxi Sheng": "山西",
"Guangxi Zhuangzu Zizhiqu": "广西",
"Liaoning": "辽宁",
"Henan Sheng": "河南",
"Guangdong": "广东",
"Tianjin Shi": "天津",
"Guizhou Sheng": "贵州"
}
var i, oriname, name, newObj = {};
obj.forEach(function(elem, index) {
console.log(elem);
oriname = elem.name;
name = geoipcity[oriname] || oriname;
elem.name = name;
if (name === oriname) {
console.log('注意:', name);
}
});
// for (var i in obj) {
// if (obj.hasOwnProperty(i)) {
// }
// }
// JSON.stringify(newObj);
// return newObj;
}
convertCity(a.hikbar);
console.log(JSON.stringify(a));
var count = 0;
a.hikbar.forEach(function(elem) {
count += elem.count
});
console.log(count);
var count2 = 0;
for (var i in a.map) {
if (a.map.hasOwnProperty(i)){
count2 += a.map[i];
};
}
console.log(count2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment