请教一个JS写法的问题

有如下一个树形结构数据,同时有一个ID数组[35,4,32],根据这个数组让下面树形结构数据除了 ID是35,4,32的都添加一个字段disabled:true,求教大神写法。

    [
        {
            "id": 1,
            "parent_id": 0,
            "department_name": "皮包公司",
            "children": [
                {
                    "id": 35,
                    "parent_id": 1,
                    "department_name": "财务部",
                },
                {
                    "id": 4,
                    "parent_id": 1,
                    "department_name": "营销部",
                    "children": [
                        {
                            "id": 47,
                            "parent_id": 4,
                            "department_name": "营销美女部",
                        }
                    ]
                },
                {
                    "id": 32,
                    "parent_id": 1,
                    "department_name": "网络部",
                    "children": [
                        {
                            "id": 42,
                            "parent_id": 3,
                            "department_name": "美工",
                        },
                        {
                            "id": 36,
                            "parent_id": 3,
                            "department_name": "前端开发",

                        },
                        {
                            "id": 41,
                            "parent_id": 3,
                            "department_name": "后端开发",
                        },
                        {
                            "id": 43,
                            "parent_id": 3,
                            "department_name": "编辑",
                        }
                    ]
                },
                {
                    "id": 37,
                    "parent_id": 1,
                    "department_name": "仓储部",
                },
            ]
        }
    ]
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案
function addDisabled(arr,ids) {
        for (let i = 0; i < arr.length; i++) {
            const element = arr[i]
            if (ids.includes(element.id)) {
                element.disabled = true;
            }
            if (element.children && element.children.length > 0) {
                addDisabled(element.children, ids);
            }
        }
        return arr;
    }
3年前 评论
讨论数量: 1
function addDisabled(arr,ids) {
        for (let i = 0; i < arr.length; i++) {
            const element = arr[i]
            if (ids.includes(element.id)) {
                element.disabled = true;
            }
            if (element.children && element.children.length > 0) {
                addDisabled(element.children, ids);
            }
        }
        return arr;
    }
3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!