data.ts 777 B

1234567891011121314151617181920212223242526272829303132
  1. import { TypeZ1dict } from './type'
  2. // 把所有的字典,只保留前面2级,给下拉框筛选
  3. export const Z1toTowFu = (data: TypeZ1dict[]) => {
  4. return data.map(item => ({
  5. ...item,
  6. children: item.children?.map(child => ({
  7. ...child,
  8. children: undefined // 移除第三层子级
  9. }))
  10. }))
  11. }
  12. // 通过id获取 树的obj
  13. export const treeResIdFu = (list: any, id: string) => {
  14. // 每次进来使用find遍历一次
  15. let res = list.find((item: any) => item.id === id)
  16. if (res) {
  17. return res
  18. } else {
  19. for (let i = 0; i < list.length; i++) {
  20. if (list[i].children instanceof Array && list[i].children.length > 0) {
  21. res = treeResIdFu(list[i].children, id)
  22. if (res) return res
  23. }
  24. }
  25. return null
  26. }
  27. }