如何给树形的数据每一项都添加一个key属性(key属性与id想同)


userListRes: [

            {

                className: "全部",

                defaultValue: "全部",

                id: "0",

                parentKey: "0",

                staticValue: '300',

                isEditable: false,

                children: [

                    {

                        className: "航天咨询",

                        id: "0-1",

                        staticValue: '300',

                        defaultValue: "航天咨询",

                        isEditable: false,

                        children: [

                            {

                                className: "航天咨询一",

                                id: "0-1-1",

                                staticValue: '300',

                                defaultValue: "0-1-1",

                                isEditable: false,

                                children: [{

                                    staticValue: '300',

                                    className: "航天咨询",

                                    id: "0-1-1-1",

                                    defaultValue: "航天咨询",

                                    isEditable: false,

                                }]

                            },

                            {

                                staticValue: '300',

                                className: "航天咨询二",

                                id: "0-1-2",

                                defaultValue: "0-1-2",

                                isEditable: false,

                            },

                        ],

                    },]

那你就要递归

recursion(list) {
 list.forEach(item => {
   item.key = id;
   if(item.children) {
      this.recursion(item.children)
   }
 })
},

递归循环一下 呗

let userListRes = [
        {
            className: "全部",
            defaultValue: "全部",
            id: "0",
            parentKey: "0",
            staticValue: '300',
            isEditable: false,
            children: [
                {
                    className: "航天咨询",
                    id: "0-1",
                    staticValue: '300',
                    defaultValue: "航天咨询",
                    isEditable: false,
                    children: [
                        {
                            className: "航天咨询一",
                            id: "0-1-1",
                            staticValue: '300',
                            defaultValue: "0-1-1",
                            isEditable: false,
                            children: [{
                                staticValue: '300',
                                className: "航天咨询",
                                id: "0-1-1-1",
                                defaultValue: "航天咨询",
                                isEditable: false,
                            }]
                        },
                        {
                            staticValue: '300',
                            className: "航天咨询二",
                            id: "0-1-2",
                            defaultValue: "0-1-2",
                            isEditable: false,
                        },
                    ],
                },]
        }
    ];
   console.log(loop(userListRes))
    function loop(data) {
       data.map((item) => {
            item.key = item.id;
            if (item.children && item.children.length > 0) {
                loop(item.children);
            }
        })
        return data;
    }