赞
踩
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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。