多层树级关系的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)