Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何循环遍历树节点中的对象数组_Jquery_Typescript - Fatal编程技术网

Jquery 如何循环遍历树节点中的对象数组

Jquery 如何循环遍历树节点中的对象数组,jquery,typescript,Jquery,Typescript,我想将数据转换为特定的树节点格式 有没有一种方法(typescript或Jquery)可以循环遍历每个对象及其子对象和子对象,并更改格式 当前数据格式 所以结果应该是这样的 { "content": [ { "data": { "dimension": "2019-12-13" }, "children": [ { "data": { "dimension": "2019-

我想将数据转换为特定的树节点格式

有没有一种方法(typescript或Jquery)可以循环遍历每个对象及其子对象和子对象,并更改格式

当前数据格式

所以结果应该是这样的

{
  "content": [
    {
      "data": {
        "dimension": "2019-12-13"
      },
      "children": [
        {
          "data": {
            "dimension": "2019-12-13"
          },
          "children": [
            {
              "data": {
                "dimension": "2019-12-13"
              },
              "children": []
            }
          ]
        }
      ]
    }
   ]
}

考虑到您正在处理一个类似树的数据结构,您可能需要一个递归函数

让我们将我们正在处理的类型正式化:

type Node1 = { // current node structure
  dimension: string
  subList: Node1[]
}

type Node2 = { // desired node structure
  data: {
    dimension: string
  }
  children: Node2[]
}
您的递归函数将如下所示:

const mapNode = (n: Node1): Node2 => {
  return {
    data: {
      dimension: n.dimension
    },
    children: n.subList.map(mapNode)
  }
}

您好,通过链接可以帮助您
const mapNode = (n: Node1): Node2 => {
  return {
    data: {
      dimension: n.dimension
    },
    children: n.subList.map(mapNode)
  }
}