javascript - 从对象属性递归生成文件路径

标签 javascript arrays node.js object recursion

我正在使用node.js,作为一个副项目,我正在创建一个读取.json文件的模块,解析它,然后根据对象属性对象值创建目录结构.

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

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

对象也将是动态的,正如用户创建的那样。

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...
];

最佳答案

下面是一个关于如何递归编写此代码的示例:

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);

关于javascript - 从对象属性递归生成文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37825878/

相关文章:

javascript - 切片拼接数组

angularjs - NodeJs Passport isAuthenticated() 即使在登录后也返回 false

javascript - 循环内的 Jasmine 异步测试未按预期工作

javascript - 在 javascript 中执行指令的成本有什么普遍性吗?

javascript - Angular UI-Router 和嵌套状态

更改结构数组元素中的值

ios - ReloadData() 不刷新 TableView

mysql - 无法读取未定义的属性 'query' - MySQL NodeJS

javascript - 为什么broadcastEval()不返回公会的角色?

javascript - 在顶部创建一个 div 将所有其他内容向下推