admin 管理员组

文章数量: 887007

[‘D303‘,‘A303‘,‘A304‘,‘A305‘,‘A306‘,‘B303‘,‘C303‘,‘A403‘,‘B403‘]创建对象排序

data() {return {data:['D303','A303','A304','A305','A306','B303','C303','A403','B403'],items:[]};
},
mounted() {let latter = [],//截取首字母items={};this.data = [...this.data].sort();//排序this.data.map(ele=>{let firstLatter = ele.substr(0,1);//首字母latter = [...new Set([...[...latter,...firstLatter]])]})latter.map(ele=>{//遍历字母对象items[ele] = []})this.data.map(ele=>{//向对象对应数组传值let firstLatter = ele.substr(0,1);//首字母items[firstLatter].push(ele)})console.log(items)
},

考虑到上面的不太好遍历,小改了下

let latter = [],//截取首字母items=[];
this.data = [...this.data].sort();//排序
this.data.map(ele=>{let firstLatter = ele.substr(0,1);//首字母latter = [...new Set([...[...latter,...firstLatter]])]
})
latter.map(ele=>{//遍历字母对象items.push({latter:ele,data:[]})
})
this.data.map(ele=>{//向对象对应数组传值let firstLatter = ele.substr(0,1);//首字母items[latter.indexOf(firstLatter)].data.push(ele)//向对应的data塞值
})
console.log(items)

本文标签: D303 A303 A304 A305 A306