vue webapp之music(十一) 歌手数据处理和singer类的封装

1.规范化处理数据(有时后端给到我们的数据并不是我们所需要的,所以需要自己对数据进行整理)

_normalizeSinger(list) {
      let map = {
        hot: {
          //热门数据
          title: HOT_NAME,
          items: []
        }
      }
      list.forEach( (item, index) => {
        if (index < HOT_NAME_LEN) {
         // items.push(item[index])
          map.hot.items.push(new Singer({
            id: item.Fsinger_mid,  //  图片拼接地址
            name: item.Fsinger_name //名字
          }))
        }
        //根据字母做聚类
        const key = item.Findex
        if (!map[key]) {
          map[key] = {
            title: key,
            items: []
          }
        }
        map[key].items.push(new Singer({
          id: item.Fsinger_mid,  //  图片拼接地址
          name: item.Fsinger_name //名字
        }))
      })
     //为了得到有序列表,我们需要处理map
      let hot = []
      let ret = []
      for (let key in map) {
        let val = map[key]
        if(val.title.match(/[a-zA-Z]/)) {
          ret.push(val)
        } else if(val.title === HOT_NAME) {
          hot.push(val)
        }
      }
      ret.sort((a,b) => {
        return a.title.charCodeAt(0) - b.title.charCodeAt(0)
      })
      return hot.concat(ret)
    }

2.新建一个singer类,并将singer类引入所需要的组件中

export default class Singer{
  constructor({id, name}) {
    this.id = id
    this.name = name
    this.avatar = `https://y.gtimg.cn/music/photo_new/T001R300x300M000${id}.jpg?max_age=2592000`
  }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值