zoukankan      html  css  js  c++  java
  • JS首字母进行分类合并加排序

    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])//将分类好的每个对象 合并在一个数组里面
        }
    }
    
  • 相关阅读:
    cf Round 633
    Django学习手册
    Django学习手册
    Django学习手册
    Django学习手册
    Django学习手册
    Django学习手册
    ERROR CL .exe……错误
    DLL、lib等链接库文件的使用
    HTTP协议
  • 原文地址:https://www.cnblogs.com/xiaobaiv/p/12079103.html
Copyright © 2011-2022 走看看