递归处理数据的几种方式


原始数据格式

image-20210225134817116

treeJson转换成一维数组


import { isEmpty, isArray } from 'lodash'

export function formatMenu(data) {
  const result = data.map((item) => {
    if (item.children && item.children.length) {
      formatMenu(item.children)
    } else {
      // console.log(item)
      const { icon, title } = item.meta
      console.log({ icon, title })
      // return { icon, title }
    }
  })
  // return result
}

image-20210225134753060

返回TreeJson相同格式,提取其中的某些数据

// import { cosh } from 'core-js/fn/number'
import { isEmpty, isArray } from 'lodash'

export function formatMenu(data) {
  const result = []
  data.forEach((item) => {
    const { icon, title } = item.meta
    if (item.children && item.children.length) {
      result.push({
        icon,
        title,
        children: formatMenu(item.children),
      })
    } else {
      result.push({ icon, title })
    }
  })
  return result
}

image-20210225134659633


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM