Skip to content

getNodeFromTree

根据唯一标志获取树形中的节点

参数

参数名参数类型参数说明
targetRecord<string, any>[] & Record<string, any>树结构对象数组或一个树结构对象
markstring | number要找的节点的唯一标志
options?Partial<Record<'idKey' | 'childrenKey', string>>标志的 key 、子集的 key

源代码&使用

ts
import { getNodeFromTree } from '@manzhixing/utilsxy';

const provinceList = [
  {
    id: "1000",
    label: "湖北省",
    children: [
      {
        id: "1001",
        pid: "1000",
        label: "武汉",
        children: [
          { id: "100101", pid: "1001", label: "洪山区" },
          { id: "100102", pid: "1001", label: "武昌区" },
          { id: "100103", pid: "1001", label: "汉阳区" },
        ],
      },
      { id: "1020", pid: "1000", label: "咸宁" },
      { id: "1022", pid: "1000", label: "孝感" },
      { id: "1034", pid: "1000", label: "襄阳" },
      { id: "1003", pid: "1000", label: "宜昌" },
    ],
  },
  {
    id: "1200",
    value: "江苏省",
    label: "江苏省",
    children: [
      { id: "1201", pid: "1200", label: "南京" },
      { id: "1202", pid: "1200", label: "苏州" },
      { id: "1204", pid: "1200", label: "扬州" },
    ],
  },
];

const province1201 = getNodeFromTree(provinceList, "1201");

{
    id: "1201",
    pid: "1200",
    label: "南京"
};
ts
/*
 * @Author: Chengbotao
 * @Contact: https://github.com/chengbotao
 */
/*
 * @Author: Chengbotao
 * @Contact: https://github.com/chengbotao
 */

export function getNodeFromTree(
  target: Record<string, any>[] & Record<string, any>,
  mark: string | number,
  options?: Partial<Record<'idKey' | 'childrenKey', string>>
) {
  const copyTree = Array.isArray(target) ? Array.prototype.slice.call(target) : [target];
  const defaultOpts = Object.assign(
    {
      idKey: 'id',
      childrenKey: 'children',
    },
    options
  );
  const { idKey, childrenKey } = defaultOpts;

  while (copyTree.length) {
    const node = copyTree.shift();
    if (node[idKey] === mark) {
      return node;
    }
    if (node[childrenKey]) {
      Array.prototype.push.apply(copyTree, node.children);
    }
  }

  return null;
}