(function () { var myApp = angular.module('myApp', []); myApp.controller('myController', function ($scope) { //트리 변환 메서드 var treeModel = function (arrayList, rootId) { var rootNodes = []; var traverse = function (nodes, item, index) { if (nodes instanceof Array) { return nodes.some(function (node) { if (node.id === item.parentId) { node.children = node.children || []; return node.children.push(arrayList.splice(index, 1)[0]); } return traverse(node.children, item, index); }); } }; while (arrayList.length > 0) { arrayList.some(function (item, index) { if (item.parentId === rootId) { return rootNodes.push(arrayList.splice(index, 1)[0]); } return traverse(rootNodes, item, index); }); } return rootNodes; }; //트리로 변환할 배열 var list = [ { label : "subUser1", id : "role11", parentId : "role1" }, { label : "subUser2-1-1", id : "role1211", parentId : "role121" }, { label : "subUser2-1-2", id : "role1212", parentId : "role121" }, { label : "Admin", id : "role2", parentId : null }, { label : "subUser2", id : "role12", parentId : "role1" }, { label : "subUser2-1", id : "role121", parentId : "role12" }, { label : "Guest", id : "role3", parentId : null }, { label : "User", id : "role1", parentId : null } ]; $scope.list = JSON.stringify(list, null, ' '); //트리 모델로 변환 var tree = treeModel(list, null); $scope.tree = JSON.stringify(tree, null, ' '); }); })();
<div ng-app="myApp"> <div ng-controller="myController"> <h3>변환할 배열 모델</h3> <pre> {{::list}} </pre> <h3>변환된 트리 모델</h3> <pre> {{tree}} </pre> </div> </div>
pre { padding:10px; background-color:#EEE; border-radius:5px; line-height: 120%; color: #555; font-size: 12px; }