js 递归数组中id相同查找对应name(children)

398 阅读1分钟
const deepByIdSearchName = (list, id, key) => {
  if (list.length) {
    for (let i = 0; i < list.length; i++) {
      if (list[i].id === id) {
        return list[i][key];
      }
      if (list[i].children && list[i].children) {
        let next = deepByIdSearchName(list[i].children, id, key);
        if (next) {
          return next;
        }
      }
    }
  }
  return;
};