<span>哈哈哈哈</span> <span>哈哈哈哈</span> <span>哈哈哈哈</span> <script> for(let i = 0; i < 26; i++){ const key = String.fromCharCode(65 + i) //A-Z赋给key当作键 map[key] = { title: key, items: [] } array.map((v,k)=>{ //遍历数组 let firstIndex = v.charAt(0).toUpperCase();//首字母 if(firstIndex.toUpperCase() == String.fromCharCode(65+i)){//统一转成大写进行逐个判断 map[key].items.push(v)//push进相对应的数组里头 } }) // //如果当前的数组里头为空,则跳过。 if(map[key].items === undefined || map[key].items.length == 0){ continue; }else{ this.newArr.push(map[key])//将分类好的每个对象 合并在一个数组里面 } } </script>
来源:https://www.cnblogs.com/xiaobaiv/p/12556696.html