본문 바로가기

javascript

재귀함수를 이용하여 json을 계층형으로 변환하기

트리맵 라이브러리에 데이터를 계층형으로 넣어야해서 찾아본 방법이다.

 

function fnTransferJson(arr, code) {
    var out = [];
    for (var i in arr) {
        if (arr[i].upprCode == upprCode) {
            var children = fnTransferJson(arr, arr[i].code);
        
            if (children.length) {
                arr[i].children = children;
            }
            out.push(arr[i]);
        }
    }
    return out;
}