js多维数组变形方法

1、二维数组转换为多维数组类

class initTreeList {
  constructor(arr, pidName, initialPid, idName) {
    this.arr = arr;
    this.formatting = arr.filter((val, ind) => {
      if (val[pidName] == initialPid) {
        if (this.findChild(val[idName], pidName, idName).length > 0) {
          val.children = this.findChild(val[idName], pidName, idName);
        }
        return true;
      } else {
        return false;
      }
    });
    return this;
  }
  findChild(id, pidName, idName) {
    return this.arr.filter((val, ind) => {
      if (val[pidName] == id) {
        if (this.findChild(val[idName], pidName, idName).length > 0) {
          val.children = this.findChild(val[idName], pidName, idName);
        }
        return true;
      } else {
        return false;
      }
    });
  }
}

实例化使用这个类

let treeList = new initTreeList(this.dataArr, "pid", 0, "id");
console.log(treeList.formatting);

传参说明

参数说明
arr原始2维数组
pidName父id字段
initialPid第一级数据的父id
idNameid字段

返回值说明

返回值说明
arr原始2维数组
formatting格式化后的多维数组

附带一组测试数据

      dataArr: [
        { id: 1, pid: 0 },
        { id: 2, pid: 0 },
        { id: 3, pid: 4 },
        { id: 4, pid: 0 },
        { id: 5, pid: 1 },
        { id: 6, pid: 4 },
        { id: 7, pid: 2 },
        { id: 8, pid: 2 },
        { id: 9, pid: 4 },
        { id: 10, pid: 4 },
        { id: 11, pid: 2 },
        { id: 12, pid: 1 },
        { id: 13, pid: 4 },
        { id: 14, pid: 3 },
        { id: 15, pid: 10 },
      ],

2、多维数组转换为二维数组类

class initList {
  constructor(arr, childrenName) {
    this.arr = arr;
    this.formatting = [];
    this.getChild(arr, childrenName);
  }
  getChild(arrChild, childrenName) {
    arrChild.forEach((val, ind) => {
      this.formatting.push(val);
      if (val[childrenName]) {
        this.getChild(val[childrenName], childrenName);
      }
    });
  }
}

实例化这个类

let data=new initList(this.options,'children')
console.log(data.formatting);

传参说明

参数说明
arr原始多维数组
childrenName子集字段

返回值说明

返回值说明
arr原始多维数组
formatting格式化后的二维数组

附带一组测试数据

	options: [
        {
          value: "zhinan",
          label: "指南",
          children: [
            {
              value: "shejiyuanze",
              label: "设计原则",
              children: [
                {
                  value: "yizhi",
                  label: "一致",
                },
              ],
            },
            {
              value: "daohang",
              label: "导航",
              children: [
                {
                  value: "cexiangdaohang",
                  label: "侧向导航",
                },
              ],
            },
          ],
        },
        {
          value: "zujian",
          label: "组件",
          children: [
            {
              value: "basic",
              label: "Basic",
              children: [
                {
                  value: "layout",
                  label: "Layout 布局",
                  children: [
                    {
                      value: "layout",
                      label: "Layout 布局",
                      children: [
                        {
                          value: "layout",
                          label: "Layout 布局",
                        },
                      ],
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],

3、多维数组过滤

class filterList {
  constructor(arr, childrenName, type) {
    this.arr = arr;
    this.formatting = arr.filter((val) => {
      if (val[childrenName]) {
        val[childrenName] = this.findChild(
          val[childrenName],
          childrenName,
          type
        );
      }
      return val[type];
    });
  }
  findChild(list, childrenName, type) {
    return list.filter((val) => {
      if (val[childrenName]) {
        val[childrenName] = this.findChild(
          val[childrenName],
          childrenName,
          type
        );
      }
      return val[type];
    });
  }
}

实例化这个类

let treeList = new filterList(this.list, "child", "type");
console.log(data.formatting);

传参说明

参数说明
arr原始多维数组
childrenName子集字段
type过滤表示字段(只接收Boolean类型)

返回值说明

返回值说明
arr原始多维数组
formatting格式化后的二维数组

附带一组测试数据

	list: [
        {
          name: "1",
          type: true,
          child: [
            {
              name: "8",
              type: true,
              child: [
                { name: "12", type: true, child: [] },
                { name: "13", type: false, child: [] },
              ],
            },
            { name: "9", type: true, child: [] },
            {
              name: "10",
              type: false,
              child: [{ name: "14", type: true, child: [] }],
            },
            { name: "11", type: true, child: [] },
          ],
        },
        {
          name: "2",
          type: true,
          child: [
            { name: "15", type: true, child: [] },
            {
              name: "16",
              type: true,
              child: [{ name: "17", type: false, child: [] }],
            },
          ],
        },
        {
          name: "3",
          type: true,
          child: [{ name: "18", type: true, child: [] }],
        },
        { name: "4", type: false, child: [] },
        {
          name: "5",
          type: true,
          child: [
            {
              name: "19",
              type: true,
              child: [
                {
                  name: "20",
                  type: false,
                  child: [
                    { name: "21", type: true, child: [] },
                    {
                      name: "22",
                      type: true,
                      child: [{ name: "23", type: true, child: [] }],
                    },
                  ],
                },
              ],
            },
          ],
        },
        { name: "6", type: true, child: [] },
        {
          name: "7",
          type: false,
          child: [{ name: "24", type: true, child: [] }],
        },
      ]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值