省市区联动数据获取

标题格式化vant的省市区,国内国家统计局的内容,不适用三级联动,获取辖区编号可以参考

@vant/area-data

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);

效果
效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值