多层树级关系的json,递归删除空值的数据

data =[{
        "name": "省",
                "children":[
                    {
                        "name": "市区",
                        "children": [
                            {
                             "name":"县",
                             "children":[]
                            }
                        ]
                    }
                ]
}]
//递归删除县层为空的children数组
function exec(array) {
    array.forEach(item => {
         if (item.children.length === 0) {
             delete item.children
        } else {
              exec(item.children)
        }
    })
}
exec(data);

console.log(data)

 

posted @ 2019-09-16 21:48  ExpectoPatronum—S  阅读(633)  评论(0编辑  收藏  举报