SOURCE

console 命令行工具 X clear

                    
>
console
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let list = [
      {
        "children": [
          {
            "children": [
              {
                "children": [
                  {
                    "children": [
                      {
                        "id": "3061",
                        "value": "五级选项 1"
                      }
                    ],
                    "id": "3060",
                    "value": "四级选项 1"
                  },
                  {
                    "children": [
                      {
                        "id": "3063",
                        "value": "五级选项 1"
                      }
                    ],
                    "id": "3062",
                    "value": "四级选项 2"
                  },
                  {
                    "children": [
                      {
                        "id": "3065",
                        "value": "五级选项 1"
                      }
                    ],
                    "id": "3064",
                    "value": "四级选项 3"
                  },
                  {
                    "children": [
                      {
                        "id": "3067",
                        "value": "五级选项 1"
                      },
                      {
                        "id": "3068",
                        "value": "五级选项 2"
                      },
                      {
                        "id": "3069",
                        "value": "五级选项 3"
                      },
                      {
                        "id": "3070",
                        "value": "五级选项 4"
                      }
                    ],
                    "id": "3066",
                    "value": "四级选项 4"
                  }
                ],
                "id": "2027",
                "value": "三级选项 1"
              },
              {
                "id": "2028",
                "value": "三级选项 2"
              },
              {
                "id": "2029",
                "value": "三级选项 3"
              }
            ],
            "id": "1002",
            "value": "二级选项 1"
          }
        ],
        "id": "1012",
        "value": "一级选项 1"
      },
    ]

    function flatten(list, times) {
      function flattenObj(objItem, times) {
        if (times > 0) {
          const obj = {}
          Object.keys(objItem).forEach(key => {
            if (key === 'children') {
              obj[key] = times == 1 ? [] : flatten(objItem[key], times - 1)
            } else {
              obj[key] = objItem[key]
            }
          })
          return obj
        }

      }
      return list.map(v => flattenObj(v, times))
    }
    console.log(flatten(list, 3))
  </script>
</body>

</html>