const objarr= [{ "id": 1, "isshow": true, "children": [{ "id": 2, "isshow": true, "children": [{ "id": 100268, "isshow": false, "showname": "ccccc", "name": "ccccc", "level": 3, "half": false }, { "id": 6, "isshow": true, "showname": "纯纯粹粹", "name": "纯纯粹粹", "level": 3, "half": false }, { "id": 100500, "isshow": true, "showname": "666", "name": "666", "level": 3, "half": false }], "showname": "1.1猜猜猜等到", "name": "1.1猜猜猜等到", "level": 2, "half": true }], "showname": "嗯哼", "name": "1", "level": 1, "half": true }, { "id": 3, "isshow": true, "children": [{ "id": 4, "isshow": true, "showname": "2.1", "name": "2.1", "level": 2, "half": false }, { "id": 5, "isshow": true, "showname": "纯纯粹粹纯纯粹粹", "name": "纯纯粹粹纯纯粹粹", "level": 2, "half": false }], "showname": "2", "name": "2", "level": 1, "half": false }, { "id": 100121, "isshow": true, "children": [{ "id": 100122, "isshow": true, "showname": "vvv v", "name": "vvv v", "level": 2, "half": false }], "showname": "单位名称", "name": "单位名称", "level": 1, "half": false }]
过滤出属性isshow为true的数据,处理上面数据,处理方法如下
convert (arr) { const newArr = arr.filter(item => item.isshow) return newArr.map(item => { if (item.children) { item.children = this.convert(item.children) } return item }) } const arr = convert(objarr) 简单的方法就实现了
————————————————
智一面|前端面试必备练习题