<script>
    let labels = [{"id": 2, "name": "艾瑞", "label": "A"}, {"id": 4, "name": "大时代", "label": "D"}, {"id": 1, "name": "道合顺", "label": "D"}, {"id": 5, "name": "小米", "label": "X"},{"id": 3, "name": "知乎", "label": "Z"}];
    function alpha_sort(labels){
       let alpha_dict = new Object();
       let factories = new Array();
       for(var i=0;i<labels.length;i++){
          let factory = labels[i];
          let label = factory.label;
          if(alpha_dict[label] == undefined){
             factories.push({"label": label, "children": [factory]});
             alpha_dict[label] = factories.length -1;
          }else {
               factories[alpha_dict[label]].children.push(factory);
          }
       }
       console.log(alpha_dict);
       console.log(factories);
       return factories;
    }
    console.log(alpha_sort(labels))
  </script>

  

posted on 2020-09-19 10:16  前端入门菜鸟  阅读(3607)  评论(0编辑  收藏  举报