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