zoukankan      html  css  js  c++  java
  • JS 数组对象根据某一相同key合并成新的数组

    <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>
    

      

  • 相关阅读:
    Rsa加密类
    Des加密类
    AES对称加密解密类
    用字符串生成二维码
    ViedoUtil获取视频的缩略图
    UmUtils得到友盟的渠道号
    UiUtils
    StringUtils
    ShockUtil振动工具类
    SHA加密
  • 原文地址:https://www.cnblogs.com/qq364735538/p/13695189.html
Copyright © 2011-2022 走看看