常用工具类收集

131 阅读1分钟

常用工具类收集

1. 将树形平级数组转成树形结构数组

rebuildTree(data) {
      const result = [] // 存放结果集
      const itemMap = {} //
      for (const item of data) {
        const id = item.id
        const pid = item.parentId

        if (!itemMap[id]) {
          itemMap[id] = {
            children: []
          }
        }

        itemMap[id] = {
          ...item,
          children: itemMap[id]['children']
        }

        const treeItem = itemMap[id]

        if (pid === 1) {
          result.push(treeItem)
        } else {
          if (!itemMap[pid]) {
            itemMap[pid] = {
              children: []
            }
          }
          itemMap[pid].children.push(treeItem)
        }
      }
      return result
    }
			//树结构转层级树
			generateTree(data) {
				const result = []
				const map = {} //使用objectId索引每个节点,方便查找父节点
				data.forEach((item) => {
					this.defaultExpandedKeys.push(item.name)
					map[item.name] = { ...item, children: [] } //使用扩展运算符和空数组初始化节点
				})
				data.forEach((item) => {
					if (item.parentId) {
						map[item.parentId].children.push(map[item.name]) //将当前节点作为子节点添加到对应的父节点的children数组中
					} else {
						result.push(map[item.name]) //没有父节点的节点作为根节点添加到结果数组中
					}
				})
				return result
			},

2. 树形数组children长度为0,将children转为undefined

    rebuildTreeData(data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          data[i].children = undefined
        } else {
          this.rebuildTreeData(data[i].children)
        }
      }
      return data
    },

3. 树形数组添加id和pid,转成父子关系的树形数组

let idIndex = 1
function createMenuId(menus, pId) {
  menus.forEach((item) => {
    item['id'] = idIndex
    item['pId'] = pId
    if (item.children) {
      ++idIndex
      createMenuId(item.children, item.id)
    } else {
      idIndex++
    }
  })

  return menus
}

menus = createMenuId(menus, 0)

5. 树形json转数组

  //树形json转数组
  treeToArray(tree, level = 0, parentId = null, primaryKey) {
    const results = [];
    for (const key in tree) {
      if (key === primaryKey) {
        for (const item of tree[key]) {
          results.push({
            name: item,
            level: level,
            parentId: parentId,
          });
        }
      } else {
        const children = this.treeToArray(
          tree[key],
          level + 1,
          key,
          primaryKey
        );
        results.push({
          name: key,
          level: level,
          parentId: parentId,
        });
        results.push(...children);
      }
    }
    return results;
  },

7. 表单参数去空

  //表单参数去空
  formatParam(obj) {
    const param = {};
    Object.keys(obj).forEach((key) => {
      if (
        (obj[key] && !Array.isArray(obj[key])) ||
        obj[key] === 0 ||
        obj[key] === '0' ||
        (Array.isArray(obj[key]) && obj[key].length > 0)
      ) {
        param[key] = obj[key];
      }
    });
    return param;
  },

9. 根据当前树形数组和pid查找所有父级

 //根据当前menu pid查找所有的父级
    findParent(name, tree, parents = []) {
      for (let i = 0; i < tree.length; i++) {
        const node = tree[i];
        if (node.menuKey === name) {
          return [...parents, node];
        } else if (node.children) {
          const found = this.findParent(name, node.children, [
            ...parents,
            node,
          ]);
          if (found) {
            return found;
          }
        }
      }
      return null;
    },
    
			//根据当前menu pid查找所有的父级
			findParentByPid(menus) {
				let parentId = this.activeNode.parentId
				let name = this.activeNode
				menus.forEach((item) => {
					if (parentId === item.name || name === item.name) {
						this.activeParentList.push(item)
					}
					if (item.children) {
						this.findParentByPid(item.children)
					}
				})
			},    

11. 根据name查找目标节点

	findElementByName(data, name) {
				// 遍历树的每个节点
				for (let i = 0; i < data.length; i++) {
					let node = data[i]

					// 检查当前节点是否与目标名称匹配
					if (node.name === name) {
						return node
					}

					// 递归调用,查找子节点
					if (node.children.length > 0) {
						let child = this.findElementByName(node.children, name)
						if (child) {
							return child
						}
					}
				}

				// 没有找到匹配的节点
				return null
			},