树结构中字段转换

转换前:
[
    {
        "value": 12, 
        "label": "企业服务", 
        "children": [
            {
                "father_id": 12, 
                "value": 7, 
                "label": "推广营销", 
                "children": [
                    {
                        "father_id": 7, 
                        "value": 788, 
                        "label": "广告营销"
                    }, 
                    {
                        "father_id": 7, 
                        "value": 789, 
                        "label": "网络营销"
                    }
                ]
            }, 
            {
                "father_id": 12, 
                "value": 30, 
                "label": "云服务", 
                "children": [
                    {
                        "father_id": 30, 
                        "value": 776, 
                        "label": "云安全"
                    }, 
                    {
                        "father_id": 30, 
                        "value": 777, 
                        "label": "云存储"
                    }
                ]
            }
        ]
    }, 
    {
        "value": 13, 
        "label": "医疗健康", 
        "children": [
            {
                "father_id": 13, 
                "value": 992, 
                "label": "医疗机构"
            }, 
            {
                "father_id": 13, 
                "value": 993, 
                "label": "医药服务", 
                "children": [
                    {
                        "father_id": 993, 
                        "value": 994, 
                        "label": "药物研发"
                    }, 
                    {
                        "father_id": 993, 
                        "value": 995, 
                        "label": "药品生产"
                    }
                ]
            }
        ]
    }
]

 转换后:

[
                {
                    value: 12,
                    label: '企业服务',
                    children: [
                        {
                            father_id: 12,
                            value: 7,
                            label: '推广营销',
                            children: [
                                {
                                    father_id: 7,
                                    value: 788,
                                    label: '广告营销',
                                    convertLabel: '企业服务 / 推广营销 / 广告营销',
                                    convertValue: '12,7,788'
                                },
                                {
                                    father_id: 7,
                                    value: 789,
                                    label: '网络营销',
                                    convertLabel: '企业服务 / 推广营销 / 网络营销',
                                    convertValue: '12,7,789'
                                }
                            ],
                            convertLabel: '企业服务 / 推广营销',
                            convertValue: '12,7'
                        },
                        {
                            father_id: 12,
                            value: 30,
                            label: '云服务',
                            children: [
                                {
                                    father_id: 30,
                                    value: 771,
                                    label: '私有云',
                                    convertLabel: '企业服务 / 云服务 / 私有云',
                                    convertValue: '12,30,771'
                                },
                                {
                                    father_id: 30,
                                    value: 772,
                                    label: '公有云',
                                    convertLabel: '企业服务 / 云服务 / 公有云',
                                    convertValue: '12,30,772'
                                }
                            ],
                            convertLabel: '企业服务 / 云服务',
                            convertValue: '12,30'
                        }
                    ],
                    convertLabel: '企业服务',
                    convertValue: '12'
                },
                {
                    value: 13,
                    label: '医疗健康',
                    children: [
                        {
                            father_id: 13,
                            value: 992,
                            label: '医疗机构',
                            convertLabel: '医疗健康 / 医疗机构',
                            convertValue: '13,992'
                        },
                        {
                            father_id: 13,
                            value: 993,
                            label: '医药服务',
                            children: [
                                {
                                    father_id: 993,
                                    value: 994,
                                    label: '药物研发',
                                    convertLabel: '医疗健康 / 医药服务 / 药物研发',
                                    convertValue: '13,993,994'
                                },
                                {
                                    father_id: 993,
                                    value: 995,
                                    label: '药品生产',
                                    convertLabel: '医疗健康 / 医药服务 / 药品生产',
                                    convertValue: '13,993,995'
                                }
                            ],
                            convertLabel: '医疗健康 / 医药服务',
                            convertValue: '13,993'
                        }
                    ],
                    convertLabel: '医疗健康',
                    convertValue: '13'
                }
            ]

递归方法(推荐):

 function convertTree(arr, label = '', value = '') {
            for (var i = 0; i < arr.length; i++) {
                arr[i].convertLabel = label + arr[i].label + ' / ';
                arr[i].convertValue = value + arr[i].value + ',';
                if (arr[i].children) {
                    arr[i].children = this.convertTree(arr[i].children, arr[i].convertLabel, arr[i].convertValue);
                }
            }
            //返回新的数组格式
            arr.forEach(item => {
                item.convertLabel = item.convertLabel.slice(0, item.convertLabel.lastIndexOf(' / '));
                item.convertValue = item.convertValue.slice(0, item.convertValue.lastIndexOf(','));
            });
            return arr;
        }

 一般方法(不推荐):

 function changeLevelName(arr) {
            arr.forEach(first => {
                if (first.children.length > 0) {
                    first.convertValue = String(first.value);
                    first.children.forEach(second => {
                        second.label = first.label + ' / ' + second.label;
                        second.convertValue = first.convertValue + ',' + second.value;
                        if (second.children && second.children.length > 0) {
                            second.children.forEach(third => {
                                third.label = second.label + ' / ' + third.label;
                                third.convertValue = second.convertValue + ',' + third.value;
                            });
                        }
                    });
                }
            });
            return arr;
        },

 

posted @ 2021-07-15 18:25  振锋小哥  阅读(67)  评论(0)    收藏  举报