_normalizeSinger 返回的是underfined
来源:5-4 listview 基础组件的开发和应用-滚动列表实现
慕前端9142252
2020-11-26
老师您好 同样的代码 为啥我的代码返回的就是underfined
_getSingerList(){
getSingerList().then(res=>{
this.singers=this._normalizeSinger(res.data.list);
console.log(this.singers)//这里返回的是underfined
})
},
_normalizeSinger(list){
const map = {
hot:{
title:HOT_NAME,
item:[],
}
};
list.forEach((item,index) => {
if(index<HOT_SINGER_LENGTH){
map.hot.item.push(new Singer({
id:item.Fsinger_mid,
name:item.Fsinger_name
}))
}
const key =item.Findex;
if(!map[key]){
map[key]={
title:key,
item:[]
}
}
map[key].item.push(new Singer({
id:item.Fsinger_mid,
name:item.Fsinger_name,
}));
let hot= []
let ret = []
for(let key in map){
if(map[key].title.match(/[a-zA-Z]/)){
ret.push(map[key])
}else if(map[key].title===HOT_NAME){
hot.push(map[key])
}
};
ret.sort((a,b)=>{
return a.title.charCodeAt(0)-b.title.charCodeAt(0);
})
return hot.concat(ret);
});
}
写回答
2回答
-
我_PT
2020-12-02
这是我用ES6的map去整理的, 可以参考下
/** * 整理歌手列表数据 */ normalizeSinger(singerList) { let map = new Map(); let hotSingerArr = []; singerList.forEach((item, index) => { let singer = new Singer(item.Fsinger_mid, item.Fsinger_name); if (index < HOT_COUNT) { hotSingerArr.push(singer); } let singerIndex = item.Findex.toUpperCase(); if (!map.has(singerIndex)) { let validSingerIndex = singerIndex.match(/[a-zA-Z]/); if (!validSingerIndex) return; map.set(singerIndex, []); } let groupContainer = map.get(singerIndex); groupContainer.push(singer); } ); let mapArr = Array.from(map); mapArr.sort((before, after) => { let previousKeyCode = before[0].charCodeAt(0); let nextKeyCode = after[0].charCodeAt(0); return previousKeyCode - nextKeyCode; }); mapArr.unshift([HOT_NAME, hotSingerArr]); return mapArr; }
00 -
ustbhuangyi
2020-11-27
建议和我 GitHub 源码对比一下,你这个 list 遍历明显有问题。
PS: 建议提问的时候别说:“同样的代码”,因为肯定是不一样的012020-11-29
相似问题