Returns the best matching value in the json object

var jsonObj = [
    {
        id: 1,
        name: "",
        path: "/dashboard",
        children: []
    },
    {
        id: 2,
        name: "",
        path: "/auth",
        children: [
            {
                id: 3,
                name: "",
                path: "/auth/role",
                children: [
                ]
            }
        ]
    }
]
var pathStr = "/auth/role/list"

have the above two variables jsonObj and pathStr,. If there is a path attribute in jsonObj that has the same value as pathStr, then return pathStr,. If it is not the same, find the one that is closest to pathStr. For example, in this case, the final return is"/ auth/role"

.
Mar.19,2021

function trans (arr, path, cur) {
    cur = cur || ''
    for (let i = 0, len = arr.length; i < len; iPP) {
        let name = arr[i].path
        if (path.startsWith(name)) {
            let children = arr[i].children
            let result = children && children.length ? trans(children, path, name) : name
            if (result === path) return path
            if (result.length > cur.length) cur = result
        }
    }
    return cur
}
trans(jsonObj, pathStr)

it's a mess, and I don't know what I wrote

var pathStrArr = pathStr.split('/')
var key1 = 0;
var key2 = 0;

var a = {
  path:'',
  search:function(obj){
      var that = this;
    obj.forEach(function(i){
        console.log(i.path);
        var iArr = (i.path).split('/')
        for(var j in iArr){
          if(iArr[j] == pathStrArr[j])
           key1PP;
         }
        if(key1>key2){
          this.path = i.path;
          key2 = key1;
          key1 = 0;
          
        }
        if(i.children){
          that.search(i.children);
        }
      });  
  }
}

a.search(jsonObj)
console.log(path)

https://codepen.io/jx915/pen/.


DFS ideas for reference

function matchPath (pathSrc, jsonObj) {
  let bestMatch = ''
  _match(jsonObj)
  return bestMatch

  function _match (list) {
    if (!Array.isArray(list)) { return }
    for (let i = 0; i < list.length; iPP) {
      const { path = '', children } = list[i]
      if (path === pathSrc) {
        bestMatch = pathSrc
        return
      }
      if (pathSrc.startsWith(path) && path.length > bestMatch.length) {
        bestMatch = path
      }
      if (children) {
        _match(children)
      }
    }
  }
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c23c-2c2e7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c23c-2c2e7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?