JavaScript 递归遍历json串获取相关数据

简介: JavaScript 递归遍历json串获取相关数据

递归遍历json串获取相关数据

 


 

  1. 1.   测试数据

 

// 导航菜单

[

 {

   id: 1,

   parentId: 0,

   parentName: null,

   name: "首页",

   url: "/home",

   perms: null,

   requireAuth: true,

   hidden: false,

   type: 0,

   icon: "fa fa-home fa-lg",a

   orderNum: 1,

   level: 0,

   children: [

     {

       id: 2,

       parentId: 1,

       parentName: null,

       name: "首页二级菜单1",

       url: "",

       perms: null,

       requireAuth: true,

       hidden: false,

       type: 1,

       icon: "fa fa-home fa-lg",

       orderNum: 1,

       level: 0,

       children: [

         {

           id: 3,

           parentId: 2,

           parentName: null,

           name: "首页三级菜单1",

           url: "",

           perms: null,

           requireAuth: true,

           hidden: false,

           type: 1,

           icon: "fa fa-home fa-lg",

           orderNum: 1,

           level: 0,

           children: [

             {

               id: 4,

               parentId: 3,

               parentName: null,

               name: "首页四级菜单1",

               url: "/home/level4Menu1",

               perms: null,

               requireAuth: true,

               hidden: false,

               type: 0,

               icon: "fa fa-home fa-lg",

               orderNum: 1,

               level: 0,

               children: []

             }

           ]

         },

         {

           id: 5,

           parentId: 2,

           parentName: null,

           name: "首页三级菜单2",

           url: "/home/level3Menu2",

           perms: null,

           requireAuth: true,

           hidden: false,

           type: 0,

           icon: "fa fa-home fa-lg",

           orderNum: 2,

           level: 0,

           children: []

         }

       ]

     },

     {

       id: 6,

       parentId: 1,

       parentName: null,

       name: "首页二级菜单2",

       url: "",

       perms: null,

       requireAuth: true,

       hidden: false,

       type: 1,

       icon: "fa fa-home fa-lg",

       orderNum: 2,

       level: 0,

       children: [

         {

           id: 7,

           parentId: 6,

           parentName: null,

           name: "首页三级菜单3",

           url: "/home/level3Menu3",

           perms: null,

           requireAuth: true,

           hidden: false,

           type: 0,

           icon: "fa fa-home fa-lg",

           orderNum: 1,

           level: 0,

           children: []

         }

       ]

     }

   ]

 },

 {

   id: 8,

   parentId: 0,

   parentName: null,

   name: "工作台",

   url: "/workbench",

   perms: null,

   requireAuth: true,

   hidden: false,

   type: 0,

   icon: "fa fa-home fa-lg",

   orderNum: 2,

   level: 0,

   children: []

 },

 {

   id: 9,

   parentId: 0,

   parentName: null,

   name: "测试视图",

   url: "/testerView",

   perms: null,

   requireAuth: true,

   hidden: false,

   type: 0,

   icon: "fa fa-home fa-lg",

   orderNum: 3,

   level: 0,

   children: [

     {

       id: 10,

       parentId: 9,

       parentName: null,

       name: "测试视图二级菜单1",

       url: "",

       perms: null,

       requireAuth: true,

       hidden: false,

       type: 1,

       icon: "fa fa-home fa-lg",

       orderNum: 1,

       level: 0,

       children: [

         {

           id: 11,

           parentId: 10,

           parentName: null,

           name: "测试视图三级菜单1",

           url: "/testerView/level3Menu1",

           requireAuth: true,

           hidden: false,

           type: 0,

           icon: "fa fa-home fa-lg",

           orderNum: 1,

           level: 0,

           children: []

         },

         {

           id: 12,

           parentId: 10,

           parentName: null,

           name: "测试视图三级菜单2",

           url: "/testerView/level3Menu2",

           requireAuth: true,

           hidden: false,

           type: 0,

           icon: "fa fa-home fa-lg",

           orderNum: 2,

           level: 0,

           children: []

         }

       ]

     }

   ]

 },

 {

   id: 13,

   parentId: 0,

   parentName: null,

   name: "配置",

   url: "/settings",

   requireAuth: true,

   hidden: false,

   type: 0,

   icon: "fa fa-home fa-lg",

   orderNum: 4,

   level: 0,

   children: []

 },

 {

   id: 14,

   parentId: 0,

   parentName: null,

   name: "其它",

   url: "",

   requireAuth: true,

   hidden: false,

   type: 0,

   icon: "fa fa-home fa-lg",

   orderNum: 5,

   level: 0,

   children: [

     {

       id: 15,

       parentId: 14,

       parentName: null,

       name: "其它菜单",

       url: "/other",

       requireAuth: true,

       hidden: false,

       type: 0,

       icon: "fa fa-home fa-lg",

       orderNum: 1,

       level: 0,

       children: []

     }

   ]

 }

]

 

 

 

 

  1. 2.   需求1

获取菜单“路由”信息:

获取每级菜单的url,name,icon, id, requireAuth字段信息,构成节点,以及其子菜单对应字段的信息,构成子节点,要求:

如果本级菜单url为空,则不记录该级菜单相关的信息,此时,如果其子菜单url不为空,则要记录其子菜单相关字段的信息,并向上查找离该子菜单最近,并且url不为空的菜单信息,并把该菜单信息当做其父节点,形如以下

[{path:"/home ",

name: "首页",

meta: {icon: "fa fa-home fa-lg", index: 4, requireAuth: true},

children:[{path: "/home/level3Menu3",

name: "首页三级菜单3",

meta: {icon: "fa fa-home fa-lg", index: 4, requireAuth: true},

children: []},

...

]

},

...

]

 

如果本级菜单url不为空,则记录该级菜单自身的信息,作为父节点,此时,如果其子菜单url不为空,则要记录其子菜单相关字段的信息,构成子节点,否则不记录子菜单信息

 

编码

 

function getMenuRoutes(menuList = [], parent = []) {

 for (var i = 0; i < menuList.length; i++) {

   var route = {}

   if (menuList[i].url && /\S/.test(menuList[i].url)) {

     route = {

       path: menuList[i].url,

       name: menuList[i].name,

       children: [],

       meta: {

         icon: menuList[i].icon,

         index: menuList[i].id,

         requireAuth: menuList[i].requireAuth

       }

     }

     if (menuList[i].children && menuList[i].children.length >= 1) {

       getMenuRoutes(menuList[i].children, route["children"])

     }

   } else {

     if (menuList[i].children && menuList[i].children.length >= 1) {

       getMenuRoutes(menuList[i].children, parent)

     }

   }

   if (JSON.stringify(route) != "{}") {

     parent.push(route)

   }

 }

return parent

}

 

调用

   var result = getMenuRoutes(navMenuData, [])

   console.log(result)

 

结果

 

 

 

  1. 3.   需求2

 

获取每级菜单的url,name,icon, id, requireAuth字段信息,构成一级节点,要求:

如果级菜单url为空,则不记录该级菜单相关的信息

 

编码

function getMenuRoutes (menuList = [], routes = []) {

 var temp = []

 for (var i = 0; i < menuList.length; i++) {

   var route = {}

   if (menuList[i].url && /\S/.test(menuList[i].url)) {

     var route = {

       path: menuList[i].url,

       name: menuList[i].name,

       children: [],

       meta: {

         icon: menuList[i].icon,

         index: menuList[i].id,

         requireAuth: menuList[i].requireAuth

       }

     }

     routes.push(route)

   }

 

   if (menuList[i].children && menuList[i].children.length >= 1) {

     temp = temp.concat(menuList[i].children)

   }

 }

 if (temp.length >= 1) {

   getDynamicRoutes2(temp, routes)

 }

 

 return routes

 

调用

   var result = getMenuRoutes(navMenuData, [])

   console.log(result)

 

结果

 

 

 

 


目录
相关文章
|
9月前
|
JSON API 数据安全/隐私保护
深度分析淘宝卖家订单详情API接口,用json返回数据
淘宝卖家订单详情API(taobao.trade.fullinfo.get)是淘宝开放平台提供的重要接口,用于获取单个订单的完整信息,包括订单状态、买家信息、商品明细、支付与物流信息等,支撑订单管理、ERP对接及售后处理。需通过appkey、appsecret和session认证,并遵守调用频率与数据权限限制。本文详解其使用方法并附Python调用示例。
|
8月前
|
机器学习/深度学习 JSON 监控
淘宝拍立淘按图搜索与商品详情API的JSON数据返回详解
通过调用taobao.item.get接口,获取商品标题、价格、销量、SKU、图片、属性、促销信息等全量数据。
|
7月前
|
JSON API 数据格式
淘宝拍立淘按图搜索API系列,json数据返回
淘宝拍立淘按图搜索API系列通过图像识别技术实现商品搜索功能,调用后返回的JSON数据包含商品标题、图片链接、价格、销量、相似度评分等核心字段,支持分页和详细商品信息展示。以下是该API接口返回的JSON数据示例及详细解析:
|
7月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
7月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
8月前
|
JSON 缓存 自然语言处理
多语言实时数据微店商品详情API:技术实现与JSON数据解析指南
通过以上技术实现与解析指南,开发者可高效构建支持多语言的实时商品详情系统,满足全球化电商场景需求。
|
8月前
|
JSON API 数据格式
干货满满!淘宝商品详情数据,淘宝API(json数据返回)
淘宝商品详情 API 接口(如 taobao.item.get)的 JSON 数据返回示例如下
|
9月前
|
JSON 算法 安全
淘宝商品详情API接口系列,json数据返回
淘宝开放平台提供了多种API接口用于获取商品详情信息,主要通过 淘宝开放平台(Taobao Open Platform, TOP) 的 taobao.tbk.item.info.get(淘宝客商品详情)或 taobao.item.get(标准商品API)等接口实现。以下是关键信息及JSON返回示例:
|
7月前
|
JSON 中间件 Java
【GoGin】(3)Gin的数据渲染和中间件的使用:数据渲染、返回JSON、浅.JSON()源码、中间件、Next()方法
我们在正常注册中间件时,会打断原有的运行流程,但是你可以在中间件函数内部添加Next()方法,这样可以让原有的运行流程继续执行,当原有的运行流程结束后再回来执行中间件内部的内容。​ c.Writer.WriteHeaderNow()还会写入文本流中。可以看到使用next后,正常执行流程中并没有获得到中间件设置的值。接口还提供了一个可以修改ContentType的方法。判断了传入的状态码是否符合正确的状态码,并返回。在内部封装时,只是标注了不同的render类型。再看一下其他返回的类型;
366 3
|
7月前
|
JSON Java Go
【GoGin】(2)数据解析和绑定:结构体分析,包括JSON解析、form解析、URL解析,区分绑定的Bind方法
bind或bindXXX函数(后文中我们统一都叫bind函数)的作用就是将,以方便后续业务逻辑的处理。
452 3