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)
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be56c5-39b23.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be56c5-39b23.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?