1 Star 0 Fork 13

Roc / js.tree

forked from 阿森 / js.tree 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
repairWith.md 1.55 KB
一键复制 编辑 原始数据 按行查看 历史
zhengxs 提交于 2022-07-28 11:45 . feat(operators): 添加 repairWith 方法

根据不完整关系修复缺失的节点数据

可以使用 repairWith 修复不完整的列表关系数据为树结构数据。

回调函数参数:

  • list - 不完整的列表关系数据
  • index - 在同级中的索引
  • parents - 所有上级的节点对象
import { repairWith } from '@zhengxs/js.tree'

const valuesMap = {
  10000: { id: 10000, parentId: null, title: '研发中心' },
  11000: { id: 11000, parentId: 10000, title: '体验设计部' },
  11001: { id: 11001, parentId: 11000, title: '设计组' },
  11002: { id: 11002, parentId: 11000, title: '前端组' },
  12000: { id: 12000, parentId: 10000, title: '系统集成部' },
}

const data = [
  { id: 12000, parentId: 10000, title: '系统集成部' },
  { id: 11001, parentId: 11000, title: '设计组' },
  { id: 11002, parentId: 11000, title: '前端组' },
]

repairWith(data, {
  resolve: (id) => valuesMap[id],
})

输出

[
  {
    "children": [
      {
        "children": [],
        "id": 12000,
        "parentId": 10000,
        "title": "系统集成部"
      },
      {
        "children": [
          {
            "children": [],
            "id": 11001,
            "parentId": 11000,
            "title": "设计组"
          },
          {
            "children": [],
            "id": 11002,
            "parentId": 11000,
            "title": "前端组"
          }
        ],
        "id": 11000,
        "parentId": 10000,
        "title": "体验设计部"
      }
    ],
    "id": 10000,
    "parentId": null,
    "title": "研发中心"
  }
]
TypeScript
1
https://gitee.com/dllen/js.tree.git
git@gitee.com:dllen/js.tree.git
dllen
js.tree
js.tree
main

搜索帮助

53164aa7 5694891 3bd8fe86 5694891