给树状数据添加层级id

const formatAuthData = ({ authData }) => {
  const find = (arr, parentId = '') => {
    for (let i = 0; i < arr.length; i++) {
      if (Array.isArray(arr[i].children) && arr[i].children.length > 0) {
        arr[i].id = `${parentId}${i + 1}`
        find(arr[i].children, `${parentId}${i + 1}`)
      } else {
        arr[i].id = `${parentId}${i + 1}`
      }
    }
  }
  const authDataCopy = deepClone(authData)
  find(authDataCopy)
  return authDataCopy
}

data.js:

const router = [
  {
    title: '首页',
    icon: 'home',
    className: 'm-sidebar-home',
    isVisible: true,
  },
  {
    title: 'xx',
    icon: 'base',
    isVisible: true,
    key: '/sale/index/offline',
    children: [
      {
        title: 'xx',
        icon: '',
        isVisible: true,
      },   
      {
        title: 'xxx',
        icon: '',
        isVisible: true,
      },                                            
    ]
  }, 
]

posted @ 2021-07-09 15:27  徐同保  阅读(442)  评论(0编辑  收藏  举报