A method of filtering a tree structure array

The day before yesterday to do the project when he met a method of filtering the data tree, the qualified data filtering out tree data as follows:

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 filtered data attribute to true, the above data processing, the following processing method

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)
	简单的方法就实现了
Published 243 original articles · won praise 108 · views 420 000 +

Guess you like

Origin blog.csdn.net/yilanyoumeng3/article/details/101150165