源数据格式
// 格式化数据的方法
const formateCityData = (list) => {
const cityList = {};
list.forEach((item) => {
const first = item.short.substr(0, 1); // 获取城市名称拼音首字母
if (cityList[first]) {
cityList[first].push(item);
} else {
cityList[first] = [item];
}
});
const cityIndex = Object.keys(cityList).sort()
return {
cityList,
cityIndex
};
};
const { cityList, cityIndex } = formateCityData(res.body);
console.log(cityList, cityIndex);
格式化后的数据