Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

js数组多层嵌套,如何快速检索?

需要实现功能是菜单检索,根据输入moduleName匹配到某个子菜单,获取到moduleCode
数据结构是这样的image.pngimage.png

数组里每个对象是一个菜单,childModules是其子菜单
怎么样能快速检索?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

这就是嵌套结构查找元素。
试试这个?https://www.npmjs.com/package...

function getResult (result, moduleName) {
  const menu = treeTool.findNode(result, node => node.moduleName == moduleName, { children: 'childModules' })
  return menu && menu.moduleCode
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...