美文网首页
树形结构数据 替换参数名

树形结构数据 替换参数名

作者: 伱个大西瓜 | 来源:发表于2018-11-09 11:34 被阅读0次

    语言 js
    实现内容是把数据内name参数名替换成title参数名

    • 数据结构
    {
        "id": "1",
        "name": "1",
        "children": [
            {
                "id": "2",
                "name": "1.1",
                "children": [
                    {
                        "id": "3",
                        "name": "1.1.1",
                        "children": []
                    },
                    {
                        "id": "4",
                        "name": "1.1.2",
                        "children": []
                    }
                ]
            }
        ]
    }
            
    
    • 方法
    function replaceParam(data) {
        let dataList = [];
        for (let item of data) {
            if (item.children.length > 0) {
                item.children = replaceParam(item.children);
            }
            let param = {
                title: item.name,
                id: item.id,
                children: item.children
            }
            dataList.push(param)
        }
        return dataList;
    }
    
    • 效果
    {
        "id": "1",
        "title": "1",
        "children": [
            {
                "id": "2",
                "title": "1.1",
                "children": [
                    {
                        "id": "3",
                        "title": "1.1.1",
                        "children": []
                    },
                    {
                        "id": "4",
                        "title": "1.1.2",
                        "children": []
                    }
                ]
            }
        ]
    }
    

    相关文章

      网友评论

          本文标题:树形结构数据 替换参数名

          本文链接:https://www.haomeiwen.com/subject/mjinxqtx.html