好客租房135-获取并处理城市列表数据(热门处理数据)

import React from 'react'
import axios from 'axios'
//导入axios
//导入navBar组件
import { NavBar, Icon } from 'antd-mobile'
import './index.scss'

// 数据格式化的方法
// list: [{}, {}]
const formatCityData = list => {
    const cityList = {}
    // const cityIndex = []
  
    // 1 遍历list数组
    list.forEach(item => {
      // 2 获取每一个城市的首字母
      const first = item.short.substr(0, 1)
      // 3 判断 cityList 中是否有该分类
      if (cityList[first]) {
        // 4 如果有,直接往该分类中push数据
        // cityList[first] => [{}, {}]
        cityList[first].push(item)
      } else {
        // 5 如果没有,就先创建一个数组,然后,把当前城市信息添加到数组中
        cityList[first] = [item]
      }
    })
  
    // 获取索引数据
    const cityIndex = Object.keys(cityList).sort()
  
    return {
      cityList,
      cityIndex
    }
  }
class cityList extends React.Component {
	state = {
		cityList: [],
	}
	componentDidMount() {
		this.getCityList()
	}
	async getCityList() {
		const res = await axios.get('http://localhost:8080/area/city?level=1')
		console.log(res, 'resss')
		const { cityList, cityIndex } = formatCityData(res.data.body)
		console.log(cityList, cityIndex)

        const hotRes = await axios.get('http://localhost:8080/area/hot')
		console.log(hotRes, 'hotRes')
		
        cityList['hot']=hotRes.data.body
        cityIndex.unshift("hot")
        console.log(cityList, cityIndex,"hotList")

	}

	render() {
		return (
			<div className="citylist">
				<NavBar
					className="navbar"
					mode="light"
					icon={<i className="iconfont icon-back" />}
					onLeftClick={() => this.props.histoty.push.go(-1)}
					// 导航栏右边内容
					// rightContent={[
					// 	<Icon
					// 		key="0"
					// 		type="search"
					// 		style={{ marginRight: '16px' }}
					// 	/>,
					// 	<Icon key="1" type="ellipsis" />,
					// ]}
				>
					城市选择
				</NavBar>
			</div>
		)
	}
}

export default cityList

运行结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端大歌谣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值