zoukankan      html  css  js  c++  java
  • 树形结构数据处理

      $(function () {
            const models = [
                {id: 1, title: 'hello', parent: 0},
                {id: 3, title: 'hello', parent: 1},
                {id: 4, title: 'hello', parent: 3},
                {id: 5, title: 'hello', parent: 4},
                {id: 2, title: 'hello', parent: 0},
                {id: 6, title: 'hello', parent: 4},
                {id: 7, title: 'hello', parent: 3},
                {id: 8, title: 'hello', parent: 10}
            ];
            const nestedStructure = getNestedChildren(models, 0);
            console.log(nestedStructure);
        })
     
        function getNestedChildren(arr, parent) {
            var out = []
            for (var i in arr) {
                if (arr[i].parent == parent) {
                    var children = getNestedChildren(arr, arr[i].id)
                    if (children.length) {
                        arr[i].children = children
                    }
                    out.push(arr[i])
                }
            }
            return out
        }
    <template>
      <el-tree
        :data="treeData"
        :props="defaultProps"
        accordion
        @node-click="handleNodeClick">
      </el-tree>
    </template>
     
    <script>
        export default {
            name: "Test",
          data(){
            return {
              data : [
                {id:1,parentId:0,name:"一级菜单A",rank:1},
                {id:2,parentId:0,name:"一级菜单B",rank:1},
                {id:3,parentId:0,name:"一级菜单C",rank:1},
                {id:4,parentId:1,name:"二级菜单A-A",rank:2},
                {id:5,parentId:1,name:"二级菜单A-B",rank:2},
                {id:6,parentId:2,name:"二级菜单B-A",rank:2},
                {id:7,parentId:4,name:"三级菜单A-A-A",rank:3},
                {id:8,parentId:7,name:"四级菜单A-A-A-A",rank:4},
                {id:9,parentId:8,name:"五级菜单A-A-A-A-A",rank:5},
                {id:10,parentId:9,name:"六级菜单A-A-A-A-A-A",rank:6},
                {id:11,parentId:10,name:"七级菜单A-A-A-A-A-A-A",rank:7},
                {id:12,parentId:11,name:"八级菜单A-A-A-A-A-A-A-A",rank:8},
                {id:13,parentId:12,name:"九级菜单A-A-A-A-A-A-A-A-A",rank:9},
                {id:14,parentId:13,name:"十级菜单A-A-A-A-A-A-A-A-A-A",rank:10},
              ],
              defaultProps: {
                children: 'children',
                label: 'name'
              }
            }
          },
          computed:{
            treeData(){
              let cloneData = JSON.parse(JSON.stringify(this.data))    // 对源数据深度克隆
              return cloneData.filter(father=>{               
                let branchArr = cloneData.filter(child=>father.id == child.parentId)    //返回每一项的子级数组
                branchArr.length>0 ? father.children = branchArr : ''   //如果存在子级,则给父级添加一个children属性,并赋值
                return father.parentId==0;      //返回第一层
              });
            }
          },
          methods:{
            handleNodeClick(data){
              // console.log(data)
              console.log(this.treeData)
            }
          },
          mounted(){
          }
        }
    </script>
     
    <style scoped>
     
    </style>
    const idMapping = data.reduce((acc, el, i) => {
      acc[el.id] = i;
      return acc;
    }, {});
    
    let root;
    data.forEach(el => {
      // 判断根节点
      if (el.parentId === null) {
        root = el;
        return;
      }
      // 用映射表找到父元素
      const parentEl = data[idMapping[el.parentId]];
      // 把当前元素添加到父元素的`children`数组中
      parentEl.children = [...(parentEl.children || []), el];
    });
    /**
     * 设置JSON数组对象属性(递归)
     * @param {*} treeSource 源数据
     * @param {*} key 指定键
     * @param {*} value 设置值或对象
     */
    export const setTreeJSONArrayProperty = function(treeSource, key, value) {
        if (!isArray(treeSource) || !key)
           return new Array()
        
        for (const data of treeSource) {
          if (data[key] && value) {
            data[key] = value
          } else if (isArray(data) && value) {
            setTreeJSONArrayProperty(data, key, value)
          }
        }

    /**
     * 设置路由children属性
     * @param {*} routes 
     * @param {*} path 
     * @param {*} children 
     * @returns 
     */
    export const setRouteArrayChildren = function(routes, path, children) {

      if (!isArray(routes) || !path)
         return new Array()
      
      for (const route of routes) {
        if (isArray(route.children)) {
          if (route.path === path && route.children.length === 0) {
            route.children.push(...children)
          } else {
            setRouteArrayChildren(route.children, path, children)
          }
        }
      }

      return routes
    }
    
    
  • 相关阅读:
    字符与字符串
    字符数组与字符指针
    c语言实现封装、继承和多态
    Halcon算子翻译——dev_map_par
    halcon算子翻译——dev_close_window
    Halcon算子翻译——dev_close_tool
    Halcon算子翻译——dev_close_inspect_ctrl
    Halcon算子翻译——dev_clear_window
    Halcon算子翻译——dev_update_time
    Halcon算子翻译——dev_update_pc
  • 原文地址:https://www.cnblogs.com/sybboy/p/15382574.html
Copyright © 2011-2022 走看看