Js recursion problem, please advise

let list = [
      {
      id: "1",
      title: "",
      checked: true,
      children: [{
        id: "5",
        title: "",
        checked: true,
        children: [{
          id: "6",
          title: "",
          checked: true,
        },
        {
          id: "7",
          title: "",
          checked: true,
        }]
      },
      {
        id: "8",
        title: "",
        checked: true,
        children: [{
          id: "9",
          title: "",
          checked: true,
        },
        {
          id: "10",
          title: "/",
          checked: false,
        }]
      }]
    }, 
    {
      id: "2",
      title: "",
      checked: true,
      children: [{
        id: "11",
        title: "",
        checked: false,
      }, {
        id: "12",
        title: "2",
        checked: true,
      }]
    }, 
    {
      id: "3",
      title: "",
      checked: true
    }
  ];

this is the acquired data. There are children under the object. You need to find out the corresponding object according to id.

start writing as follows:

function findIDobj(Arr,ID) {
      let result = null;
      for (let i = 0; i < Arr.length; iPP) {//1

        if (Arr[i].id == ID) {
          result = Arr[i]; //
        }

        if (Arr[i].children) { //

          for (let j = 0; j < Arr[i].children.length; jPP) { //2
            if (Arr[i].children[j].id == ID) {
              result = Arr[i].children[j];
            }

            if (Arr[i].children[j].children) { //

              for (let k = 0; k < Arr[i].children[j].children.length; kPP) {//3
                if (Arr[i].children[j].children[k].id == ID) {
                  result = Arr[i].children[j].children[k];
                }
              }
            }
          }
        }
      }
      return result
    }
    console.log(findIDobj(list,9))

seems to be able to find objects based on id, but this big code looks like low.
make changes again, as follows:

    let result = null;

    function findId(Arr, ID) {
      
      for (let i = 0; i < Arr.length; iPP) {
        if (Arr[i].id == ID) {
          result = Arr[i]
        }
        if (Arr[i].children) {
          findId(Arr[i].children, ID)
        }
      }
      return result
    }

    console.log(findId(list, 9))

Recursion finally looks like this, which can achieve the effect, but I want to seal result into the function, but the final return value defined in it is null. We also know that the reason is that when we loop, we execute the findId function to make result = null. Now it"s like asking how to put the result object inside a function without having to define result

outside the function.
May.31,2021

function findId(Arr, ID) {
  var _result = null;
  for (let i = 0; i < Arr.length; iPP) {
    //console.log(Arr[i], Arr[i].id == ID)
    if (Arr[i].id == ID) return Arr[i];
    if (Arr[i].children) _result = findId(Arr[i].children, ID)
    if (_result != null) return _result;
  }
  return _result
}

call ok with that object of yours findId (list,3)

clipboard.png

Menu