多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] #### 安装字母排序城市数据 * [ ] 饿了么H5端全国城市数据 * [ ] https://shadow.elemecdn.com/lib/city-list@0.0.3/city_list.json 1. 首先看api 接口数据返回的格式 [http://39.97.33.178/api/cityList](http://39.97.33.178/api/cityList) ![](https://box.kancloud.cn/6ff199a86d4fd2c6b064d34287a5afe9_520x632.png) 2. 根据数据结构按照城市py首字母进行排序,并筛选出热门城市 ~~~ <script> import { constants } from 'crypto'; export default { name: 'City', mounted() { this.$http.get('/api/cityList').then(res => { let data = res.data; let cities = null; if (data.msg === 'ok') { cities = data.data.cities this.formatCityList(cities) } }) }, methods: { formatCityList(cities) { // 排序后的城市 var cityList = []; // 热门城市 var hotList = []; // 热门城市过滤 hotList = cities.filter(item => item.isHot) // 按照城市拼音首字母排序 for (let i=0; i<cities.length; i++) { var firstletter = cities[i].py.substring(0,1).toUpperCase() if (toCom(firstletter)) // 新添加索引 { cityList.push( { index:firstletter, list:[ {nm:cities[i].nm, id: cities[i].id} ] } ) } else { // 累加已有索引 for (let j=0; j<cityList.length; j++) { if (cityList[j].index === firstletter) { cityList[j].list.push({ nm: cities[i].nm, id: cities[i].id }) } } } } cityList.sort((n1, n2) => { if (n1.index > n2.index) { return 1; } else if (n1.index < n2.index) { return -1; } else { return 0; } }) function toCom(firstletter) { for (let i=0; i<cityList.length; i++) { if (cityList[i].index === firstletter) { return false; } } return true; } } } } </script> ~~~