Skip to content

树转数组

将树形结构的数据转换为扁平的数组。

参数

接受两个参数:

  • Tree: 树形结构数组

  • Options: 一个可选的参数对象,用于配置转换方法的具体行为

    属性描述类型默认值
    addFields需要添加的字段名称及其对应的属性值计算方法的列表[{ fieldName: string;callback: (item) => any }][]
    childrenKey子节点的键名string'children'
    ignoreFields要忽略的字段名称列表string[][]
    needParentId是否添加节点信息的父节点 IDbooleantrue
    primaryKey节点的主键名booleantrue

示例

javascript
const treeArray = [
  {
    key: '1',
    name: 'Node 1',
    list: [
      {
        key: '2',
        name: 'Node 2',
        list: [
          {
            key: '3',
            name: 'Node 3'
          }
        ]
      },
      {
        key: '4',
        name: 'Node 4'
      }
    ]
  }
];
const calculateDepth = (node) => {
  let depth = 0;
  let parent = node;
  while (parent) {
    depth++;
    parent =
      parent['parentId'] && treeArray.find((n) => n.id === parent['parentId']);
  }
  return depth;
};
const options = {
  childrenKey: 'list',
  ignoreFields: [],
  addFields: [
    {
      fieldName: 'hasChildren', // Add a new 'field' property with a boolean value
      callback: (node) => Boolean(node['children'])
    },
    {
      fieldName: 'depth', // Add a new 'depth' property with the depth of each node
      callback: calculateDepth
    }
  ],
  needParentId: true
};

const flatArray = treeToArray(treeArray, options);

console.log(flatArray);

[ { "id": "1", "name": "Node 1", "parentId": "", "hasChildren": false, "depth": 1 }, { "id": "2", "name": "Node 2", "parentId": "1", "hasChildren": false, "depth": 1 }, { "id": "3", "name": "Node 3", "parentId": "2", "hasChildren": false, "depth": 1 }, { "id": "4", "name": "Node 4", "parentId": "1", "hasChildren": false, "depth": 1 } ]

Released under the MIT License.