26个字母匹配排序

158 阅读1分钟
    引入pinyin 插件
    import pinyin from "../../node_modules/js-pinyin/index"
    
    
    
     接口调用处理
    
    post({ 
    url: "/api/list" 
    }).then(({ data }) => { 
    let segs = []; // 存放数据 
    let res = {}; 
    let curr; 
    let list = data.data.list.map(item => { 
      return { 
        pinyin: pinyin.getCamelChars(item.name_zh).substr(0, 1), 
        ...item 
      }; 
    }); 
    let letters = "*ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""); 
    letters.filter((items, i) => { 
      curr = { 
        initials: "", //字母 
        data: [] //数据 
      }; 
      list.map(v => { 
        if (v.pinyin[0].toUpperCase() == items) { 
          curr.data.push(v); 
        } 
      }); 

      if (curr.data.length) { 
        curr.initials = letters[i]; 
        segs.push(curr); 
        curr.data.sort((a, b) => { 
          return a.pinyin.localeCompare(b.pinyin); 
        }); 
      } 
    }); 
    res.segs = Array.from(new Set(segs)); 
    
    // this.getChooseList= this.getChooseList.data

    res.segs.forEach(function(val){
      val.data.forEach(function(v){
        v.checkeds = false
      })
    })
    
    console.log(res.segs)
  });