js遞歸循環——將已有的數據處理生成一個新的數據


場景:

以下為已有數據,過濾掉數據中  selectedStatus為false的數據,並生成一組新數據(注意:不能在原數據中進行操作)

 

let treeData = [
    {
        level: 0,
        parent_id: 0,
        name: "員工管理",
        id: 2,
        status: 1,
        selectedStatus:true,
        child: [
            {
                level: 1,
                parent_id: 2,
                name: "辦理入職",
                id: 3,
                status: 1,
                selectedStatus:false,
                child: [
                    {
                        level: 2,
                        parent_id: 3,
                        name: "直接辦理入職",
                        id: 4,
                        status: 1,
                        selectedStatus:false,
                        child:[]
                    },
                ]
            },
            {
                level: 1,
                parent_id: 2,
                name: "轉正管理",
                id: 5,
                status: 1,
                selectedStatus:true,
                child: [
                    {
                        level: 2,
                        parent_id: 5,
                        name: "辦理轉正",
                        id: 6,
                        status: 1,
                        selectedStatus:true,
                        child:[]
                    }
                ]
            }
        ]
    },{
        level: 0,
        parent_id: 0,
        name: "員工管理",
        id: 2,
        status: 1,
        selectedStatus:false,
    },{
        level: 0,
        parent_id: 0,
        name: "員工管理",
        id: 2,
        status: 1,
        selectedStatus:true,
        child: [
            {
                level: 1,
                parent_id: 2,
                name: "辦理入職",
                id: 3,
                status: 1,
                selectedStatus:false,
                child:[]
            },
            {
                level: 1,
                parent_id: 2,
                name: "轉正管理",
                id: 5,
                status: 1,
                selectedStatus:true,
                child: [
                    {
                        level: 2,
                        parent_id: 5,
                        name: "辦理轉正",
                        id: 6,
                        status: 1,
                        selectedStatus:true,
                        child:[]
                    },{
                        level: 2,
                        parent_id: 5,
                        name: "辦理轉正",
                        id: 7,
                        status: 1,
                        selectedStatus:true,
                        child:[]
                    },{
                        level: 2,
                        parent_id: 5,
                        name: "辦理轉正",
                        id: 8,
                        status: 1,
                        selectedStatus:true,
                        child:[]
                    }
                ]
            }
        ]
    },
]

 

實現方式:

turn_format(list) {
   //此處將數據轉為json,然后再轉為對象,是為了避免操作原數據 let list_json
= JSON.stringify(list); let arr = JSON.parse(list_json) return arr.filter((item) => { if (!item.selectedStatus) { return false; }else{ if (item.child){ item.childthis.turn_format(item.child); delete item.selectedStatus }else{ delete item.selectedStatus } return true; } }); } //查看過濾后的數據 console.log(this.turn_format(treeData));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM