zoukankan      html  css  js  c++  java
  • js==>id-pid转children【】树结构数据

    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
    </head>
    <body>
    <script>
        var source = [
       
         { id:1,pid:0,value:"唐僧" },
         { id:2,pid:1,value:"唐僧1" },
         { id:3,pid:1,value:"唐僧2" },
         { id:4,pid:1,value:"唐僧3" },
         { id:5,pid:0,value:"孙悟空" },
         { id:6,pid:5,value:"孙悟空1" },
         { id:7,pid:5,value:"孙悟空2" },
         { id:8,pid:5,value:"孙悟空3" },
         { id:9,pid:0,value:"猪八戒" },
         { id:10,pid:9,value:"猪八戒1" },
         { id:11,pid:10,value:"猪八戒2" },
         { id:12,pid:11,value:"猪八戒3" },
         { id:13,pid:12,value:"猪八戒4" },
         { id:14,pid:13,value:"猪八戒5" },
         { id:15,pid:14,value:"猪八戒6" },
         { id:16,pid:0,value:"沙僧" }
       
       ]
         function toTree(data) {
           let result = []
           if (!Array.isArray(data)) {
             return result
           }
           data.forEach(item => {
               delete item.children;
           });
           let map = {};
           data.forEach(item => {
               map[item.id] = item;
           });
           data.forEach(item => {
             let parent = map[item.pid];
             if (parent) {
                 (parent.children || (parent.children = [])).push(item);
             } else {
                 result.push(item);
             }
           });
           return result;
         }
         console.log(toTree(source))
       </script>
    </body>
    </html>
    
    

  • 相关阅读:
    体育场馆预订系统版本1.0
    需求分析
    系统界面主地图
    详细设计
    概要设计
    测试用例正式发布
    第二次全体会议顺利召开5.30
    第一次小组会议(5.24)
    SDk编程基础
    单词canutillos祖母绿canutillos英语
  • 原文地址:https://www.cnblogs.com/jsll/p/13511050.html
Copyright © 2011-2022 走看看