Js filters the tree structure data according to a certain attribute to obtain a new tree structure.

requirements as above, the data are as follows

      data= [
        {
          id: 0,
          event: "1",
          timeLine: 50,
          comment: "",
                    age:13
        },
        {
          id: 1,
          event: "1",
          timeLine: 100,
          comment: "",
                    age:13
          children: [
            {
              id: 2,
              event: "2",
              timeLine: 10,
              comment: "",
                            age:11
            },
            {
              id: 3,
              event: "3",
              timeLine: 90,
              comment: "",
                            age:13
              children: [
                {
                  id: 4,
                  event: "4",
                  timeLine: 5,
                  comment: "",
                                    age:17
                },
                {
                  id: 5,
                  event: "5",
                  timeLine: 10,
                  comment: "",
                                    age:13
                },
                {
                  id: 6,
                  event: "6",
                  timeLine: 75,
                  comment: "",
                                    age:13
                  children: [
                    {
                      id: 7,
                      event: "7",
                      timeLine: 50,
                      comment: "",
                                            age:13
                      children: [
                        {
                          id: 71,
                          event: "71",
                          timeLine: 25,
                          comment: "xx",
                                                    age:18
                        },
                        {
                          id: 72,
                          event: "72",
                          timeLine: 5,
                          comment: "xx",
                                                    age:13
                        },
                        {
                          id: 73,
                          event: "73",
                          timeLine: 20,
                          comment: "xx",
                                                    age:15
                        }
                      ]
                    },
                    {
                      id: 8,
                      event: "8",
                      timeLine: 25,
                      comment: "",
                                            age:19
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]

now we want to filter data according to the attribute that age is 13, get a new tree structure, and find a recursive method

.
Jun.21,2022

wrote one recursively and checked the output manually seems to be no problem. Errors can occur when using other data. Anyway, the train of thought is, if there is a mistake, it should not be difficult to correct.

var newarr = []
function render (arr, newarr) {
        let i = 0
        let len = arr.length
        for (;i < len; iPP) {
          if (arr[i].age == 13){
            newarr.push({event: arr[i].event, id: arr[i].id,age:arr[i].age})
          }
          if (arr[i].children && arr[i].children.length > 0) {
            render(arr[i].children, newarr)
          }
        }
        return newarr
      }
render(data,newarr)
console.log(newarr)
Menu