Javascript traverses to get data that meets certain levels

1. The value is

let dataset = {
    "name": "a",
    "children": [
        {
            "name": "1",
            "children": [],
            "isShow": 1,
        },
        {
            "name": "2",
            "children": [
                {
                    "name": "22",
                    "isShow": 1,
                    "children": []
                },
                {
                    "name": "23",
                    "children": [
                        {
                            "name": "231",
                            "isShow": 1,
                            "children": [{
                                name: "zzz",
                                "isShow": 1,
                                children: "jjjjj"
                            }]
                        },
                        {
                            "name": "232",
                            "isShow": 1,
                            "children": []
                        }
                    ]
                },
                {
                    "name": "24",
                    "isShow": 1,
                    "children": [
                        {
                            "name": "241",
                            "isShow": 1,
                            "children": []
                        },
                        {
                            "name": "242",
                            "isShow": 1,
                            "children": []
                        },
                        {
                            "name": "243",
                            "isShow": 1,
                            "children": []
                        },
                        {
                            "name": "244",
                            "isShow": 1,
                            "children": []
                        }
                    ]
                }
            ]
        },
        {
            "name": "3",
            "isShow": 0,
            "children": [{
                "name": "31",
                "isShow": 1,
                "children": [
                    {
                        "name": "311",
                        "children": []
                    },
                    {
                        "name": "312",
                        "children": []
                    }
                ]
            }]
        }
    ]
}

the desired value is isshow 1 with a 3-tier structure

 "children": [{
                                name: "zzz",
                                "isShow": 1,
                                children: "jjjjj"
                            }] 
                            
    

    let children = dataset.children;
if (Array.isArray(children)) {
    let val = children.map((item, index) => {
        let childitemTree = item.children;
            if (Array.isArray(childitemTree)) {
                let childresult = childitemTree.map(childitem => {
                    if (Array.isArray(childitem.children)) {
                        let lastchild = childitem.children.map(lastitem => {
                            return { ...lastitem, children: [] }
                        });
                        return { ...childitem, children: [lastchild] }
                    }
                    return { ...childitem }
                });
                return { ...item, children: [childresult] }
            }


    });
}

feels that the writing is too complicated, but I can"t think of any other method without adding the judgment of isShow

.
Jul.03,2021

. Brother, recursion


there is an attribute called hasOwnproperty this attribute you want to find which test if there is no filter Filter.

Menu