码迷,mamicode.com
首页 > 编程语言 > 详细

JS首字母进行分类合并加排序

时间:2019-12-22 10:46:57      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:define   ==   push   from   items   this   fir   uppercase   index   

let array = ['fds', 'ewfg1', 'cvd', 'ew', 'qer', 'jjh', 'rth', 'asd', 'vsd', 'tteh', 'fxv'];
let map = {};
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])//将分类好的每个对象 合并在一个数组里面
    }
}

JS首字母进行分类合并加排序

标签:define   ==   push   from   items   this   fir   uppercase   index   

原文地址:https://www.cnblogs.com/xiaobaiv/p/12079103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!