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`
}
}