<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var input = { h3: { parent: 'h2', name: '副總經理(市場)' }, h1: { parent: 'h0', name: '公司機構' }, h7: { parent: 'h6', name: '副總經理(總務)' }, h4: { parent: 'h3', name: '銷售經理' }, h2: { parent: 'h1', name: '總經理' }, h8: { parent: 'h0', name: '財務總監' }, h6: { parent: 'h4', name: '倉管總監' }, h5: { parent: 'h4', name: '銷售表明' }, h0: { parent: '', name: 'root' } }; var plain2Tree = function (obj) { var key, res; for(key in obj) { var parent = obj[key].parent; if(parent === '') { res = obj[key]; } else { obj[parent][key] = obj[key]; } } return res }; console.log(plain2Tree(input)); </script> </body> </html>