标题格式化vant的省市区,国内国家统计局的内容,不适用三级联动,获取辖区编号可以参考
const fs = require('fs')
const areaList = { // 来自vant下省市区的内容
province_list: {
110000: '北京市',
120000: '天津市',
130000: '河北省',
...
},
city_list: {
110100: '北京市',
120100: '天津市',
130100: '石家庄市',
130200: '唐山市',
...
},
county_list: {
110101: '东城区',
110102: '西城区',
110105: '朝阳区',
110106: '丰台区',
...
}
}
var arr = []
for (key in areaList.province_list){
var obj = {}
obj.label = areaList.province_list[key]
obj.value = key
var id = key.substring(0,2)
var child = []
for(keys in areaList.city_list){
if(id === keys.substring(0,2)){
var obj1 = {
label: keys,
value: areaList.city_list[keys]
}
var arr2 = []
for(keyss in areaList.county_list){
if(id === keyss.substring(0,2)){
arr2.push({
label: areaList.county_list[keyss],
value: keyss
})
}
}
obj1.children = arr2
child.push(obj1)
}
}
obj.childList = child
arr.push(obj)
}
// node 执行并输出到文件area1.js
fs.writeFile('area1.js','export area = ' + JSON.stringify(arr),'utf-8',(err)=>{
if (err) return console.log(err);
console.log('写入成功')
})
附 获取政府公开数据:
有参考别人的,不,完全复制的,哈哈哈
飞机
var a = document.createElement('script');
a.src = "https://cdn.bootcdn.net/ajax/libs/jquery/2.1.1/jquery.js"
document.querySelector('body').append(a)
String.prototype.endWith=function(endStr){
var d=this.length-endStr.length;
return (d>=0&&this.lastIndexOf(endStr)==d)
}
var provinceList=[];
var provinceItem={};
var cityItem={};
$("tbody tr").each(function(){
var _codetxt=$(this).find("td").eq(1).text().trim();
var _code=parseInt(_codetxt);
var _name=$(this).find("td").eq(2).text().trim();
if(!(_code>0)){return true;}
if(_codetxt.endWith("0000"))
{
//以上个省的名义添加对应的城市
if(typeof(cityItem.name)!="undefined")
{
provinceItem.city.push(cityItem);
cityItem={};
}
if(provinceItem.city)
{
provinceList.push(provinceItem);
}
provinceItem={
name: _name,
postcode: _codetxt,
city: []
};
}else if(_codetxt.endWith("00"))
{
//将上个市数据完结掉
if(typeof(cityItem.name)!="undefined"&&cityItem.area.length<=0)
{
cityItem.area.push({
name: "市辖区",
postcode: (parseInt(cityItem.postcode)+1)+""
});
}
if(typeof(cityItem.name)!="undefined")
{
provinceItem.city.push(cityItem);
}
cityItem={
name: _name,
postcode: _codetxt,
area: []
};
}
else
{
//直辖市
if(provinceItem.name&&typeof(cityItem.name)=="undefined")
{
//区升级为city,下包含一个区/县
provinceItem.city.push({
name: _name,
postcode: _codetxt,
area: [{
name: "区",
postcode: _codetxt+"_0"
}]
});
}
else
{
//区
//console.log(_codetxt);
areaItem={
name: _name,
postcode: _codetxt
};
cityItem.area.push(areaItem);
}
}
});
console.log(provinceList);
效果