返回对象中所有属性的路径

50 阅读1分钟

{ label:'饼图', legend:{ zlevel:2, borderColor:'red', padding:{ top:10 } } }

   `
   function getPath(obj) {
      if(Object.prototype.toString.call(obj) !== '[object Object]'){
        return []
      }
      const result = [];
      const getKeyPath=(obj,res,parent)=>{
        for(const key in obj){
          let path = key
          if(parent){
            path = `${parent}.${key}`
          }
          result.push(path)
          let val = obj[key]
          if(Object.prototype.toString.call(val) === '[object Object]'){
            getKeyPath(val,res,path)
          }
        }
      }
      getKeyPath(obj,result)
      return result;
    }
   `