Javascript 使用mysql结果集在json中创建文件夹结构层次结构

Javascript 使用mysql结果集在json中创建文件夹结构层次结构,javascript,mysql,node.js,hierarchy,directory-structure,Javascript,Mysql,Node.js,Hierarchy,Directory Structure,下表用于存储与子id关联的我的文件夹结构父id,如果子id没有任何父id,则为0,我需要特定用户的所有文件夹结构 # id, cid, pid, userid '1', '2', '1', '1' '2', '3', '2', '1' '3', '4', '8', '1' '4', '5', '3', '1' '5', '6', '3', '1' '6', '7', '6', '1' '7', '8', '7', '1' '8', '9', '7', '1' '9', '10', '8', '1

下表用于存储与子id关联的我的文件夹结构父id,如果子id没有任何父id,则为0,我需要特定用户的所有文件夹结构

# id, cid, pid, userid
'1', '2', '1', '1'
'2', '3', '2', '1'
'3', '4', '8', '1'
'4', '5', '3', '1'
'5', '6', '3', '1'
'6', '7', '6', '1'
'7', '8', '7', '1'
'8', '9', '7', '1'
'9', '10', '8', '1'
'10', '11', '0', '2'
'11', '12', '11', '2'
'12', '13', '0', '2'
'13', '1', '0', '1'
用户的结果集=1

select * from rec where userid=1
# id, cid, pid, userid
'1', '2', '1', '1'
'2', '3', '2', '1'
'3', '4', '8', '1'
'4', '5', '3', '1'
'5', '6', '3', '1'
'6', '7', '6', '1'
'7', '8', '7', '1'
'8', '9', '7', '1'
'9', '10', '8', '1'
'13', '1', '0', '1'
现在我需要生成这个数据的json层次结构,如

{
    1:[{
        2:[(
            3:[{
                5:[
                ],
                6:[{
                    7:[{
                        8:[{
                            10:[]
                        }],
                        9:[]
                    }]
                }]
            }]
        }]
    }]

}

嗯?显示的结果集不需要递归函数。“现在我需要节点中的文件夹结构层次结构”-因此请显示实际所需的输出。请展示您目前编写的代码。事实上,保存您的输入的实际JS结构。1:[{2:[{3:[{5:[{}]6:[{7:[{8:[{}]}]}]}]}]为什么它不需要递归函数,如果不需要,那么如何获得上面的json,因为每个cild都相互依赖