首页 > 其他分享 >js 树形结构 数组 互转

js 树形结构 数组 互转

时间:2022-09-21 18:33:25浏览次数:78  
标签:categoryCode categoryCnName 90000 pid js A1 树形 互转 id

let arr = [{
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "111",
    "bdictDesc": "测试1",
    "pid": "0",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "222",
    "bdictDesc": "测试2",
    "pid": "0",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "333",
    "bdictDesc": "测试3",
    "pid": "0",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "444",
    "bdictDesc": "测试4",
    "pid": "0",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "555",
    "bdictDesc": "测试5",
    "pid": "0",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA",
    "bdictDesc": "测试6",
    "pid": "0",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA01",
    "bdictDesc": "测试7",
    "pid": "DOCA",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA0101",
    "bdictDesc": "测试8",
    "pid": "DOCA01",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA0102",
    "bdictDesc": "测试9",
    "pid": "DOCA01",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA0103",
    "bdictDesc": "测试10",
    "pid": "DOCA01",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA02",
    "bdictDesc": "测试11",
    "pid": "DOCA",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA03",
    "bdictDesc": "测试12",
    "pid": "DOCA",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA0301",
    "bdictDesc": "测试13",
    "pid": "DOCA03",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA0302",
    "bdictDesc": "测试14",
    "pid": "DOCA03",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA04",
    "bdictDesc": "测试15",
    "pid": "DOCA",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA05",
    "bdictDesc": "测试16",
    "pid": "DOCA",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA06",
    "bdictDesc": "测试17",
    "pid": "DOCA",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA07",
    "bdictDesc": "测试18",
    "pid": "DOCA",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA08",
    "bdictDesc": "测试19",
    "pid": "DOCA",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA09",
    "bdictDesc": "测试20",
    "pid": "DOCA",
    "validStatus": "1"
}, {
    "categoryCode": "90000",
    "categoryCnName": "A1级别",
    "id": "DOCA10",
    "bdictDesc": "测试21",
    "pid": "DOCA",
    "validStatus": "1"
}, ]

function arrayToTree(list, parentID) {
    const child = function(pareID) {
        //先定义一个数组,用于存储所查到的子元素
        const childs = [];
        //循环数组
        for (let i = 0; i < list.length; i++) {
            //如果数组其中一项的pid等于传入的,说明这一项是传入的子元素,把他push进数组,然后重复递归自己找该项的子元素
            if (list[i].pid === pareID) {
                if (child(list[i].id).length > 0) {
                    list[i].children = child(list[i].id);
                }
                childs.push(list[i]);
            }
        }
        //最后将查到的所有子元素返回
        return childs;
    };
    return child(parentID);
}



let tree22 = [
    {
        "categoryCode": "90000",
        "categoryCnName": "A1级别",
        "id": "111",
        "bdictDesc": "测试1",
        "pid": "0",
        "validStatus": "1"
    },
    {
        "categoryCode": "90000",
        "categoryCnName": "A1级别",
        "id": "222",
        "bdictDesc": "测试2",
        "pid": "0",
        "validStatus": "1"
    },
    {
        "categoryCode": "90000",
        "categoryCnName": "A1级别",
        "id": "333",
        "bdictDesc": "测试3",
        "pid": "0",
        "validStatus": "1"
    },
    {
        "categoryCode": "90000",
        "categoryCnName": "A1级别",
        "id": "444",
        "bdictDesc": "测试4",
        "pid": "0",
        "validStatus": "1"
    },
    {
        "categoryCode": "90000",
        "categoryCnName": "A1级别",
        "id": "555",
        "bdictDesc": "测试5",
        "pid": "0",
        "validStatus": "1"
    },
    {
        "categoryCode": "90000",
        "categoryCnName": "A1级别",
        "id": "DOCA",
        "bdictDesc": "测试6",
        "pid": "0",
        "validStatus": "1",
        "children": [
            {
                "categoryCode": "90000",
                "categoryCnName": "A1级别",
                "id": "DOCA01",
                "bdictDesc": "测试7",
                "pid": "DOCA",
                "validStatus": "1",
                "children": [
                    {
                        "categoryCode": "90000",
                        "categoryCnName": "A1级别",
                        "id": "DOCA0101",
                        "bdictDesc": "测试8",
                        "pid": "DOCA01",
                        "validStatus": "1"
                    },
                    {
                        "categoryCode": "90000",
                        "categoryCnName": "A1级别",
                        "id": "DOCA0102",
                        "bdictDesc": "测试9",
                        "pid": "DOCA01",
                        "validStatus": "1"
                    },
                    {
                        "categoryCode": "90000",
                        "categoryCnName": "A1级别",
                        "id": "DOCA0103",
                        "bdictDesc": "测试10",
                        "pid": "DOCA01",
                        "validStatus": "1"
                    }
                ]
            },
            {
                "categoryCode": "90000",
                "categoryCnName": "A1级别",
                "id": "DOCA02",
                "bdictDesc": "测试11",
                "pid": "DOCA",
                "validStatus": "1"
            },
            {
                "categoryCode": "90000",
                "categoryCnName": "A1级别",
                "id": "DOCA03",
                "bdictDesc": "测试12",
                "pid": "DOCA",
                "validStatus": "1",
                "children": [
                    {
                        "categoryCode": "90000",
                        "categoryCnName": "A1级别",
                        "id": "DOCA0301",
                        "bdictDesc": "测试13",
                        "pid": "DOCA03",
                        "validStatus": "1"
                    },
                    {
                        "categoryCode": "90000",
                        "categoryCnName": "A1级别",
                        "id": "DOCA0302",
                        "bdictDesc": "测试14",
                        "pid": "DOCA03",
                        "validStatus": "1"
                    }
                ]
            },
            {
                "categoryCode": "90000",
                "categoryCnName": "A1级别",
                "id": "DOCA04",
                "bdictDesc": "测试15",
                "pid": "DOCA",
                "validStatus": "1"
            },
            {
                "categoryCode": "90000",
                "categoryCnName": "A1级别",
                "id": "DOCA05",
                "bdictDesc": "测试16",
                "pid": "DOCA",
                "validStatus": "1"
            },
            {
                "categoryCode": "90000",
                "categoryCnName": "A1级别",
                "id": "DOCA06",
                "bdictDesc": "测试17",
                "pid": "DOCA",
                "validStatus": "1"
            },
            {
                "categoryCode": "90000",
                "categoryCnName": "A1级别",
                "id": "DOCA07",
                "bdictDesc": "测试18",
                "pid": "DOCA",
                "validStatus": "1"
            },
            {
                "categoryCode": "90000",
                "categoryCnName": "A1级别",
                "id": "DOCA08",
                "bdictDesc": "测试19",
                "pid": "DOCA",
                "validStatus": "1"
            },
            {
                "categoryCode": "90000",
                "categoryCnName": "A1级别",
                "id": "DOCA09",
                "bdictDesc": "测试20",
                "pid": "DOCA",
                "validStatus": "1"
            },
            {
                "categoryCode": "90000",
                "categoryCnName": "A1级别",
                "id": "DOCA10",
                "bdictDesc": "测试21",
                "pid": "DOCA",
                "validStatus": "1"
            }
        ]
    }
]

let treeRes = [];

function tree2arr(res) {
    res.forEach(item => {
        if (!item.children) {
            treeRes.push(item)
        } else {
            let obj = {
                ...item,
                children: null
            }
            delete obj.children
            treeRes.push(obj)
            tree2arr(item.children)
        }
    })
}
// 此处的res取的是上边的数组转树的结果
tree2arr(tree22)
console.log(treeRes);

标签:categoryCode,categoryCnName,90000,pid,js,A1,树形,互转,id
From: https://www.cnblogs.com/7c89/p/16716705.html

相关文章

  • 记录nodejs中querystring‘已弃用’三种处理方法
    一.升级node版本,修改引入方式//升级到18.x版本后修改引入方式constquerystring=require('node:querystring')二.官方推荐URLSearchParams替代因为不想升级就按照......
  • JS 动态获取 Url 参数(封装函数)
     话不多说直接上代码:封装函数如下:(如果urls固定,可以只写一个name变量)getQueryString(name,urls){varurl=urls;//获取URL......
  • js 中常用的format函数
    js中经常要写dom和数据拼接的情况,下面这种format传参方式可以解决。$.format=function(source,params){if(arguments.length==1)returnfunctio......
  • 直播网站程序源码,JS:判断对象中是否有数据
    直播网站程序源码,JS:判断对象中是否有数据 Object.keys(对象).length可以判断对象中是否有数据,length为0则表示没有数据,length为1则表示有1条数据,以此类推比如: let......
  • JS 验证自定义计算公式是否成功
    1、需求1.项目需求表单中填写公式传入后台。2.公式中可以使用汉字英文。3.验证公式是否成功。2、思路1.表单中输入公式存入后台是以字符串的形式传入。2.循环字符串......
  • 30. Python json模块
    1.前言JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,遵循欧洲计算机协会制定的JavaScript规范(简称ECMAScript)。JSON易于人阅读和编写,同时也易于机器解......
  • Js中的File对象
    概述JavaScript中存在File对象,允许网页中的JavaScript访问其内容。File对象通常是用户在网页中的一个<input>元素上传文件返回的FileList对象,或者是拖放操作返回......
  • 如何保持 NextJS 项目的清晰和干净
    如何保持NextJS项目的清晰和干净已经使用NextJS实现了许多项目,我真的不得不说,我喜欢它。您构建Web应用程序的速度,尤其是与Tailwind结合使用的速度令人难以置信。......
  • JS/TS算法---回溯算法
    回溯算法(backtracking)、什么是回溯法回溯法也可以叫做回溯搜索法,它是一种搜索的方式。在二叉树系列中,我们已经不止一次,提到了回溯,例如二叉树:以为使用了递归,其实还隐藏......
  • GSON搞定任何JSON数据
    一、Gson介绍GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可以将一个Json字符转成一个Java对象,或者将一个Java转化为Json字符串。特点:快速......