有相同属性的对象放在同一个数组:
_nomarSinger (list) { //传入一个list数组,对数组中的数据进行封装
let map = {
hot: { //热门
title: HOT_NAME,
items: []
}
};
console.log(list);
list.forEach((item, index) => {
if(index < HOT_NAME_LEN){
map.hot.items.push(new Singer(
item.Fsinger_id,
item.Fsinger_name,
item.Fsinger_mid
));
}
const key = item.Findex;
if(!map[key]){ // 按key来进行分类
map[key] = {
title: key,
items: []
}
}
map[key].items.push(new Singer(
item.Fsinger_id,
item.Fsinger_name,
item.Fsinger_mid
));
});
console.log(map);
}
封装Singer类:
export default class Singer {
constructor(id, name, mid) {
this.id = id;
this.name = name;
this.avator = `https://y.gtimg.cn/music/photo_new/T001R300x300M000${mid}.jpg?max_age=2592000`
}
}
最后得到的结果: