js操作对象的问题?

问题描述

如何处理这个数组(menuItems)的第一项,得到这样(result)的格式的对象,就是删去部分属性

    var result={
        name:"Baseinfo",
        path:'/baseinfo',
        component:'Home',
        children: [
          {
              name: "Baseinfo_School",
              path:'/baseinfo/shcool',
              component:'Baseinfo_School',
              children:[
                  {
                    name: "Baseinfo_test",
                    path:'/baseinfo/test',
                    component:'Baseinfo_Test',
                  }
              ]
          },
          {
              name: "Baseinfo_Teacher",
              path:'/baseinfo/teacher',
              component:'Baseinfo_School',
          },
        ]
    }
menuIteams: [
            {
              //组件名  
              name: "Baseinfo",
              icon: "f-menu",
              lable: "基本信息管理",
               //路由地址
              path:'/baseinfo',
              //要使用的组件
              component:'Home',
              children: [
                {
                  name: "Baseinfo_School",
                  icon: "s-menu",
                  lable: "学校基本信息维护",
                  path:'/baseinfo/shcool',
                  //要使用的组件
                  component:'Baseinfo_School',
                  children:[
                      {
                        name: "Baseinfo_test",
                        icon: "s-menu",
                        lable: "学校基本信息维护",
                        path:'/baseinfo/test',
                        //要使用的组件
                        component:'Baseinfo_Test',
                      }
                  ]
                },
                {
                  name: "Baseinfo_Teacher",
                  icon: "s-menu",
                  lable: "教师基本信息维护",
                 
                  path:'/baseinfo/teacher',
                  //要使用的组件
                  component:'Baseinfo_School',
                },
              ]
            }
          ]
阅读 1.3k
1 个回答

其实就是遍历对象,对每一项数据进行操作。给一个遍历的函数吧:

function treeForeach (tree, func) {
  let node, list = [...tree]
  while (node = list.shift()) {
    func(node)
    node.children && list.push(...node.children)
  }
}

把你的menuTeams作为参数传入即可:

treeForEach(menuTeams, node => {
  // 删除不需要的属性
  delete node.xxx
})
let result = menuTeams[0]

其实就是树结构的遍历操作,更多关于树结构操作:http://wintc.top/article/20

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题