js 实现将扁平化数据转换为渲染树形结构所需的数据

Posted monkeyfly007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 实现将扁平化数据转换为渲染树形结构所需的数据相关的知识,希望对你有一定的参考价值。

/*
 * @Description: 
 * @version: 0630
 * @Author: YaoPengFei582
 * @Date: 2021-06-30 16:27:17
 * @LastEditors: YaoPengFei582
 * @LastEditTime: 2021-06-30 17:55:12
 */
const flatTree = [
  { id: "1-1", label: "1-1级", parentId: "1" },
  { id: "2-1", label: "2-1级", parentId: "2" },
  { id: "3-1", label: "3-1级", parentId: "3" },
  { id: "1-2", label: "1-2级", parentId: "1" },
  { id: "2-2", label: "2-2级", parentId: "2" },
  { id: "3-2", label: "3-2级", parentId: "3" },
  { id: "1-3", label: "1-3级", parentId: "1" },
  { id: "2-3", label: "2-3级", parentId: "2" },
  { id: "3-3", label: "3-3级", parentId: "3" },
  { id: "1", label: "1级", parentId: "" },
  { id: "2", label: "2级", parentId: "" },
  { id: "3", label: "3级", parentId: "" },
  { id: "1-1-1", label: "1-1-1级", parentId: "1-1" },
  { id: "2-1-1", label: "2-1-1级", parentId: "2-1" },
  { id: "3-1-1", label: "3-1-1级", parentId: "3-1" },
]

const resultTree = [
  {
    id: \'1\',
    parentId: "",
    label: "1级",
    children: [
      {
        id: "1-1", label: "1-1级", parentId: "1", children: [
          { id: "1-1-1", label: "1-1-1级", parentId: "1-1", children: [] },
          { id: "1-1-2", label: "1-1-2级", parentId: "1-1", children: [] },
        ]
      },
      {
        id: "1-2", label: "1-2级", parentId: "1", children: []
      },
    ]
  }, {
    id: \'2\',
    parentId: "",
    label: "2级",
    children: []
  }, {
    id: \'3\',
    parentId: "",
    label: "3级",
    children: []
  },
]

const firstLevelTree = flatTree.filter(node => !node.parentId)
firstLevelTree.forEach(node => {
  node.children = getChildNodeList(node.id, flatTree)
})

// 递归调用创建 ChildNodeList
function getChildNodeList(id, tree) {
  const childNodeList = tree.filter(node => node.parentId === id)
  return childNodeList.map(childNode => {
    return {
      ...childNode,
      children: getChildNodeList(childNode.id, tree)
    }
  })
}
console.log(\'firstLevelTree\', firstLevelTree)

转换后的结果:

const result = [
  {
    "id": "1",
    "label": "1级",
    "parentId": "",
    "children": [
      {
        "id": "1-1",
        "label": "1-1级",
        "parentId": "1",
        "children": [
          {
            "id": "1-1-1",
            "label": "1-1-1级",
            "parentId": "1-1",
            "children": []
          }
        ]
      },
      {
        "id": "1-2",
        "label": "1-2级",
        "parentId": "1",
        "children": []
      },
      {
        "id": "1-3",
        "label": "1-3级",
        "parentId": "1",
        "children": []
      }
    ]
  },
  {
    "id": "2",
    "label": "2级",
    "parentId": "",
    "children": [
      {
        "id": "2-1",
        "label": "2-1级",
        "parentId": "2",
        "children": [
          {
            "id": "2-1-1",
            "label": "2-1-1级",
            "parentId": "2-1",
            "children": []
          }
        ]
      },
      {
        "id": "2-2",
        "label": "2-2级",
        "parentId": "2",
        "children": []
      },
      {
        "id": "2-3",
        "label": "2-3级",
        "parentId": "2",
        "children": []
      }
    ]
  },
  {
    "id": "3",
    "label": "3级",
    "parentId": "",
    "children": [
      {
        "id": "3-1",
        "label": "3-1级",
        "parentId": "3",
        "children": [
          {
            "id": "3-1-1",
            "label": "3-1-1级",
            "parentId": "3-1",
            "children": []
          }
        ]
      },
      {
        "id": "3-2",
        "label": "3-2级",
        "parentId": "3",
        "children": []
      },
      {
        "id": "3-3",
        "label": "3-3级",
        "parentId": "3",
        "children": []
      }
    ]
  }
]

以上是关于js 实现将扁平化数据转换为渲染树形结构所需的数据的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 扁平与树形数组数据的转换

将扁平的分层数据转换为树状结构的 JSON

JS中树形对象与数组之间的相互转换

Javascript中扁平化数据结构与JSON树形结构转换详解

js中平级数组和树形结构数据相互转换

树形数据结构和扁平数据相互转换