从对象属性recursion地生成文件path

我正在使用node.js和作为一个副项目,我创build一个模块,读取一个.json文件,parsing它,然后创build基于object propertiesobject values目录结构。

对象属性(keys)将是它自己的path/文件和对象值将是该path的文件列表

我试图通过对象递减递减,但我不知道如何从每个对象的最内层对象中提取path

同样,对象将是由用户创build的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... ]; 

下面是一个关于如何recursion编写的例子:

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

我对这个问题的解决办法如下:

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