【问题标题】:recursively generate filepaths from object properties从对象属性递归生成文件路径
【发布时间】:2016-10-16 00:06:26
【问题描述】:

我正在使用 node.js,作为一个辅助项目,我正在创建一个读取 .json 文件的模块,对其进行解析,然后根据 object propertiesobject values 创建目录结构。

对象属性(keys) 将是其自身/文件的路径,对象值将是该路径的文件列表

我试图通过对象向下递归,但我不知道如何从每个对象的最内层对象中提取路径

对象也是dynamic,由用户创建。

var path = 'c:/templates/<angular-app>';


var template = { 
  //outline of 'angular-app'
  src:{
    jade:['main.jade'],
    scripts:{
      modules:{
        render:['index.js'],
        winodws:['index.js'],
        header:['header.js' ,'controller.js'],
        SCSS:['index.scss' ,'setup.scss'],
      }
    }
  },
  compiled:['angular.js','angular-material.js' ,'fallback.js'],
  built:{
    frontEnd:[],//if the array is empty then create the path anyways
    backEnd:[],
    assets:{
      fontAwesome:['font-awesome.css'],
      img:[],
      svg:[]
    }
  }
}

//desired result...
let out = [
  'c:/template name/src/jade/main.jade',
  'c:/template name/src/scripts/index.js',
  'c:/template name/src/scripts/modules/render/index.js',
  'c:/template name/compiled/angular.js',
  'c:/template name/compiled/angular-material.js',
  'c:/template name/compiled/fallback.js',
  'c:/template name/built/frontEnd/',
  'c:/template name/built/backEnd/',
  //...ect...
];

【问题讨论】:

    标签: javascript arrays node.js object recursion


    【解决方案1】:

    这是一个关于如何递归编写的示例:

    var path = 'c:/templates';
    
    var template = {
      //outline of 'angular-app'
      src: {
        jade: ['main.jade'],
        scripts: {
          modules: {
            render: ['index.js'],
            winodws: ['index.js'],
            header: ['header.js', 'controller.js'],
            SCSS: ['index.scss', 'setup.scss'],
          }
        }
      },
      compiled: ['angular.js', 'angular-material.js', 'fallback.js'],
      built: {
        frontEnd: [], //if the array is empty then create the path anyways
        backEnd: [],
        assets: {
          fontAwesome: ['font-awesome.css'],
          img: [],
          svg: []
        }
      }
    }
    
    function recurse(item, path, result) {
      //create default output if not passed-in
      result = result || [];
    
      //item is an object, iterate its properties
      for (let key in item) {
        let value = item[key];
        let newPath = path + "/" + key;
    
        if (typeof value === "string") {      
          //if the property is a string, just append to the result
          result.push(newPath + "/" + value);      
        } else if (Array.isArray(value)) {      
          //if an array
          if (value.length === 0) {
            //just the directory name
            result.push(newPath + "/");
          } else {
            //itearate all files
            value.forEach(function(arrayItem) {
              result.push(newPath + "/" + arrayItem);
            });
          }
        } else {
          //this is an object, recursively build results
          recurse(value, newPath, result);
        }
      }
    
      return result;
    }
    
    var output = recurse(template, path);
    console.log(output);

    【讨论】:

      【解决方案2】:

      我对这个问题的解决方案如下;

      function getPaths(o, root = "", result = []) {
        var ok = Object.keys(o);
        return ok.reduce((a,k) => { var p = root + k + "/";
                                    typeof o[k] == "object" && o[k] !== null &&
                                    Array.isArray(o[k]) ? o[k].length ? o[k].forEach(f => a.push(p+=f))
                                                                      : a.push(p)
                                                        : getPaths(o[k],p,a);
                                    return a;
                                  },result);
      }
      var path = 'c:/templates/',
      template = { 
        //outline of 'angular-app'
        src:{
          jade:['main.jade'],
          scripts:{
            modules:{
              render:['index.js'],
              winodws:['index.js'],
              header:['header.js' ,'controller.js'],
              SCSS:['index.scss' ,'setup.scss'],
            }
          }
        },
        compiled:['angular.js','angular-material.js' ,'fallback.js'],
        built:{
          frontEnd:[],//if the array is empty then create the path anyways
          backEnd:[],
          assets:{
            fontAwesome:['font-awesome.css'],
            img:[],
            svg:[]
          }
        }
      },
         paths = getPaths(template,path);
      console.log(paths);

      这只是一个名为getPaths 的简单函数,实际上它有一个非常基本的递归运行。如果您的对象结构良好(不包含除对象和数组以外的任何属性且没有空值),您甚至可以删除typeof o[k] == "object" &amp;&amp; o[k] !== null &amp;&amp; 行。抱歉我的非正统缩进风格,但这就是我发现在使用 ES6 箭头回调执行三元、逻辑快捷方式和数组方法时更容易处理代码的方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-01
        • 2020-11-18
        • 2015-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多