SOURCE

const x = 3;
const y = 2;
const z = 1;
const gData = [];

const generateData = (_level, _preKey, _tns) => {
  const preKey = _preKey || '0';//【step2】未指定开始参数时,从0开始。一般是子级有指定,比如指定0-0开始
  const tns = _tns || gData;//【step3】未指定输出数组时,输出到gData;指定时,即对应所在级中的children

  const children = [];//【step4】这里的children仅代表拥有children的级数
  for (let i = 0; i < x; i++) {//【step5】当前“级数”的值从0生成到2为止即<3。比如最终,一级树只有0-0、0-1、0-2
    const key = `${preKey}-${i}`;//【step6】未指定时,比如生成0-0;指定0-0时,比如生成0-0-0
    tns.push({ title: key, key });
    if (i < y) {//【step7】如果当前“级数”的值小于2,给children添加一份当前key,以供遍历
      children.push(key);//--也代表当前级的当前index是有children的
    }
  }

  if (_level < 0) {//【step-end】递归的停止标记。1-1=0 -> 0-1<0
    return tns;
  }
  const level = _level - 1;
  children.forEach((key, index) => {
    tns[index].children = [];
    return generateData(level, key, tns[index].children);//【step8】对当前集数的children进行递归
  });
};
generateData(z);//【step1】从第一级开始生成

console.log(JSON.stringify(gData))
console.log("===")
const dataList = [];
const generateList = (data) => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const { key } = node;
    dataList.push({ key, title: key });
    if (node.children) {
      generateList(node.children);
    }
  }
};
generateList(gData);
console.log(JSON.stringify(dataList))

console 命令行工具 X clear

                    
>
console