递归

70 阅读1分钟

递归是什么?

link:baijiahao.baidu.com/s?id=173674…

递归,就是在运行的过程中不断地调用自己。递归有两个过程,简单地说一个是递的过程,一个是归的过程。

获取树形数据层级路径问题

数据源

listData: [
        {
          id: "1",
          name: "test1",
          children: [
            {
              id: "1-1",
              name: "test1-1",
              children: [
                {
                  id: "1-1-1",
                  name: "test1-1-1",
                  children: [],
                },
              ],
            },
            {
              id: "1-2",
              name: "test1-2",
              children: [
                {
                  id: "1-2-1",
                  name: "test1-2-1",
                  children: [],
                },
              ],
            },
          ],
        },
        {
          id: "2",
          name: "test2",
          children: [],
        },
        {
          id: "3",
          name: "test3",
          children: [],
        },
        {
          id: "4",
          name: "test4",
          children: [
            {
              id: "4-1",
              name: "test4-1",
              children: [],
            },
            {
              id: "4-2",
              name: "test4-2",
              children: [
                {
                  id: "4-2-1",
                  name: "test4-2-1",
                  children: [],
                },
                {
                  id: "4-2-2",
                  name: "test4-2-2",
                  children: [
                    {
                      id: "4-2-2-1",
                      name: "test4-2-2-1",
                      children: [],
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],

函数

getPath(list, arr = []) {
      for (let i in list) {
        let el = list[i];
        arr.push(el.id);
        if (el.id === "4-2-2-1") {
          return { id: el.id };
        } else if (el.children && el.children.length) {
          let ars = this.getPath(el.children, arr);
          if (ars) {
            return ars;
          }
        }
        arr.pop();
      }
    },
    
// this.getPath(this.listData)

扁平数据转树形结构

数据源

listData: [
        {
          id: 1,
          pid: 0,
          name: "test1",
        },
        {
          id: 2,
          pid: 1,
          name: "test2",
        },
        {
          id: 3,
          pid: 1,
          name: "test3",
        },
        {
          id: 4,
          pid: 2,
          name: "test4",
        },
        {
          id: 5,
          pid: 2,
          name: "test5",
        },
        {
          id: 6,
          pid: 5,
          name: "test6",
        },
      ],

实现函数

getTree(list) {
      let keys = {};
      let tree = [];
      list.forEach((el, index) => {
        el.children = [];
        let { id } = el;
        keys[id] = el;
      });
      for (let i in list) {
        let el = list[i];
        let newItem = keys[el.id];
        if (el.pid in keys) {
          keys[el.pid]["children"].push(el);
        } else {
          tree.push(newItem);
        }
      }
      return tree;
    },

树形结构转扁平数据

数据源

listDataCopy: [
        {
          id: 1,
          pid: 0,
          name: "test1",
          children: [
            {
              id: 2,
              pid: 1,
              name: "test2",
              children: [
                {
                  id: 4,
                  pid: 2,
                  name: "test4",
                  children: [],
                },
                {
                  id: 5,
                  pid: 2,
                  name: "test5",
                  children: [
                    {
                      id: 6,
                      pid: 5,
                      name: "test6",
                      children: [],
                    },
                  ],
                },
              ],
            },
            {
              id: 3,
              pid: 1,
              name: "test3",
              children: [],
            },
          ],
        },
      ],

方法

daping(list, arr = []) {
      list.forEach((el) => {
        arr.push(el);
        if (el.children && el.children.length) {
          this.daping(el.children, arr);
        }
      });
      return arr;
    },