The data returned from QQ music is not what we want, so we need to process res.data.list to facilitate dom operation
_getSingerList () {
getSingerList().then((res) => {
if (res.code === ERR_OK) {
this.singer = res.data // The data is not object Type so change to object
this.list = this._nomalSingerList(this.singer.list)
// console.log(this.list)
}
})
},
_nomalSingerList(list){
//Define an object that containshotandkeyMultiple objects
let map = {
hot: {
title: 'Popular',
items: []
}
}
//ergodiclist, takelistData classification of
list.forEach((item, index) = >{
if (index < 10) { // take index Top 10 pop singers
map.hot.item.push(new Singer({ // Defined Singer Class
name: item.Fsinger_name, // This is the data returned
id: item.Fsinger_mid
})
}
// Definition map Object with the other first name of
const key = item.Findex // The returned data is the initial of the singer name
if (!map.[key]){ // If there is no object with this initial, a new object is created
map[key] = {
title: key,
items: []
}
}
map.[key].push(new Singer({ // Divide all singers into exclusive objects
name: item.Fsinger_name,
id: item.Fsinger_mid
}))
})
// by map Object to sort elements in
let hotSinger = [] // Set up two arrays to put data
let retSinger = []
// ergodic map object
for(let key in map){
let val = map[key]
if (val.title.match(/[a-zA-Z]/) { // if title yes a-z,Just push reach retSinger Array, other (i.e title by'Popular')push reach hotSinger
retSinger.push(val)
} else {
hotSinger.push(val)
}
}
// Yes ret Array to sort
retSinger.sort(a, b, () => {
return a.title.charCodeAt(0) - b.title.charCodeAt(0) // Ascending order
}
//Whole function returns a large array
return hotSinger.concat(retSinger) // Merge two small arrays and return
}
Finally, we get a large array, which is convenient for dom operation
The charCodeAt() method returns the Unicode encoding of the character at the specified location. The return value is an integer between 0 - 65535.
The array.sort() method defaults to ascending sort. If you want to sort according to other standards, you need to provide a comparison function, which compares two values (a, b), and then returns a number that describes the relative order of the two values. The comparison function should have two parameters a and B with the following return values:
If a is less than B, it is in ascending order. In the sorted array, a should appear before B, then - 1 is returned. return a-b
If a equals b, 0 is returned.
If a is greater than b, 1 is returned.