The tree array fetches the item whose ID is 3. The simpler the method is, the better.

const floor = [
  {
    id: 1,
    text: "",
    children: [
      {
        id: 2,
        text: "",
        children: [
          {
            id: 3,
            text: "",
            children: [
              {
                id: 4,
                text: ""
              }
            ]
          }
        ]
      }
    ]
  }
];

let result = null;

const findItemById = (id, list = floor) => {
  list.some(e => {
      if (e.id === id) {
          result = e;
          return true;
      } else {
          if (e.children) {
            result = findItemById(id, e.children);
            if (result) {
              return true;
            } else {
              return false;
            }
          }
      }
  })
  return result;
}

console.log(findItemById(3))

look at the data regularly and be lazy

        function getId(id, obj = floor) {
            for (let i = 1; i < id; iPP) {
                obj = obj[0]["children"]
            }
            return obj[0]
        }
        console.log(getId(3))

function getId (floor, id) {
  for (const val of floor) {
    return val.id === id ? val : val.children ? getId(val.children, id) : false
  }
  return false
}

console.log(getId(floor, 3))

support infinite pole nesting with recursive simplicity

const floor = [
  {
    id: 1,
    text: '',
    children: [
      {
        id: 2,
        text: '',
        children: [
          {
            id: 3,
            text: '',
            children: [
              {
                id: 4,
                text: ''
              }
            ]
          }
        ]
      }
    ]
  }
];
const getById = (list, id) =>{
    for(let i = 0; i < list.length; iPP) {
        if(list[i].id === id){
            return list[i]
        }else {
            if(list[i].children){
                return getById(list[i].children, id)
            }
        }
    }
}

console.log(getById(floor, 3))

function findValue (arr) {
  return arr[0].id === 3 ? arr[0] : findValue(arr[0].children)
}
console.log(findValue(floor))

   

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-1e3c8ac-64cdb.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-1e3c8ac-64cdb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?