demand
Navigation bar or menu bar components, metadata from the outermost layer to the target vlaue on the path of all text highlighted. So we need to figure out what are the paths that go through Target Vlaue?
example
- The metadata results are as follows:
Const dataSource = [{dataSource: ", value: 1,}, {dataSource: ", value: 2, child: [{dataSource: ", value: 1,}, child: [{dataSource: ", value: 1,}, child: [{label: 'women ', value: 211,},],}, {label:' women ', value: 22, child: [{label: 'women ', value: 211,},],}, {label:' women ', value: 22, child: [{label: 'women ', value: 221, child: [{label: 'Stewed noodles ', value: 2211,}, {label:' Stewed noodles ', value: 2212,},],}, {label: 'Stewed noodles ', value: 2212,},],}, {label:' Stewed noodles ', value: 2212,}, value: 222,},],}]}, {label: ", value: 3, child: [{label: ", value: 31,}, {label: ", value: 33, child: [{label: 'all orders, value: 331}, {label:' for the goods, the value: 332,},],}}]]
- Find all paths to the metadata:
/** ** const getAllValuePaths = (dataSource) = bb0 {let result = [] if (! dataSource || dataSource.length ===0) return [] const constructPaths = (data, path) => { data.forEach(({ value, child }) => { path.push(value) if (! child || child.length === 0) { result.push(JSON.parse(JSON.stringify(path))) } else { constructPaths(child, path) } path.pop() }) } constructPaths(dataSource, []) return result }
- Optimize all path methods to find the path for the specified target value:
/** * getValuePassByTarget = (dataSource, target) => {let result = [] if (! dataSource || dataSource.length ===0 || ! target) return [] const constructPaths = (data, target, path) => { data.forEach(({ value, child }) => { path.push(value) if (value === target) { result = JSON.parse(JSON.stringify(path)) return } if (child && child.length) { constructPaths(child, target, path) } path.pop() }) } constructPaths(dataSource, target, []) return result }
- Self-test results: