简体   繁体   English

JSON文件递归迭代返回“ undefined”和找到的值

[英]JSON file recursive iterating is returning 'undefined' and the found values

I'm trying to iterate through a json file to build a folder and file structure from a template. 我正在尝试遍历json文件以从模板构建文件夹和文件结构。 However when I specify a specific key to log it logs both the keys, AND the values I want. 但是,当我指定要记录的特定键时,它会记录两个键以及所需的值。 Is there any way to fix this issue? 有什么办法可以解决此问题? Why is it even doing this? 为什么还要这样做?

 function scan(obj) { if (obj instanceof Object) { for (var k in obj) { if (obj.hasOwnProperty(k)) { scan(obj[k]); var val = obj[k]; console.log(val.dir + '/' + val.file+ '.'+val.ext + '\\n'); } } } } json = [ { "dir": ".github", "nodes": [ { "file": "ISSUE_TEMPLATE", "ext": "md", "data": true } ] }, { "dir": ".vscode", "nodes": [ { "file": "extensions", "ext": "json" }, { "file": "settings", "ext": "json" } ] }, { "dir": "app", "nodes": [ { "dir": "src", "nodes": [ { "dir": "crash", "nodes": [ { "dir": "styles", "nodes": [ { "file": "crash", "ext": "scss" } ] }, { "file": "index", "ext": "tsx" } ] }, { "dir": "lib" }, { "dir": "main-process", "nodes": [ { "dir": "menu", "nodes": [ { "file": "index", "ext": "ts" } ] }, { "file": "main", "ext": "ts" } ] }, { "dir": "models" }, { "dir": "shared-process", "nodes": [ { "file": "index", "ext": "ts" } ] }, { "dir": "ui", "nodes": [ { "file": "index", "ext": "tsx" } ] } ] }, { "dir": "static", "nodes": [ { "dir": "common" }, { "dir": "logos" }, { "file": "error", "ext": "html", "data": true }, { "file": "index", "ext": "html", "data": true } ] }, { "dir": "styles", "nodes": [ { "dir": "mixins" }, { "dir": "ui", "nodes": [ { "file": "_app-menu-bar", "ext": "scss" }, { "file": "_focus", "ext": "scss" }, { "file": "_title-bar", "ext": "scss" } ] }, { "file": "_globals", "ext": "scss" }, { "file": "_mixins", "ext": "scss" }, { "file": "_type", "ext": "scss" }, { "file": "_ui", "ext": "scss" }, { "file": "_variables", "ext": "scss" }, { "file": "_vendor", "ext": "scss" }, { "file": "appname", "ext": "scss" } ] }, { "dir": "test" }, { "file": "package", "ext": "json", "data": true }, { "file": "webpack.common", "ext": "js" }, { "file": "webpack.development", "ext": "js" }, { "file": "webpack.production", "ext": "js" } ] }, { "dir": "docs", "nodes": [ { "dir": "contributing", "nodes": [ { "file": "setup", "ext": "md" }, { "file": "styleguide", "ext": "md" }, { "file": "tooling", "ext": "md" }, { "file": "troubleshooting", "ext": "md" } ] }, { "dir": "process", "nodes": [ { "file": "issue-triage", "ext": "md" }, { "file": "releasing-updates", "ext": "md" }, { "file": "reviews", "ext": "md" }, { "file": "roadmap", "ext": "md" } ] }, { "dir": "technical" }, { "file": "installation", "ext": "md" }, { "file": "README", "ext": "md" } ] }, { "dir": "script", "nodes": [ { "file": "build" }, { "file": "debug" }, { "file": "dist-info", "ext": "js" }, { "file": "package" }, { "file": "publish" }, { "file": "run", "ext": "js" }, { "file": "start" } ] }, { "file": ".gitmodules" }, { "file": ".travis", "ext": "yml" }, { "file": "appveyor", "ext": "yml" } ] scan(json) 

You need to specify the parent directory in your recursive function otherwise the parent dir will always be null. 您需要在递归函数中指定父目录,否则父目录将始终为null。 Also you should pass only your nodes array to the next call rather than the whole subdocument : 另外,您应该只将nodes数组传递给下一个调用,而不是整个子文档:

 function scan(parent, obj) { for (var k in obj) { if (obj[k]) { if (obj[k].nodes && obj[k].dir) { scan(parent + '/' + obj[k].dir, obj[k].nodes); } if (obj[k].file && obj[k].ext) { console.log(parent + '/' + obj[k].file + '.' + obj[k].ext + '\\n'); } } } } json = [{ "dir": ".github", "nodes": [{ "file": "ISSUE_TEMPLATE", "ext": "md", "data": true }] }, { "dir": ".vscode", "nodes": [{ "file": "extensions", "ext": "json" }, { "file": "settings", "ext": "json" } ] }, { "dir": "app", "nodes": [{ "dir": "src", "nodes": [{ "dir": "crash", "nodes": [{ "dir": "styles", "nodes": [{ "file": "crash", "ext": "scss" }] }, { "file": "index", "ext": "tsx" } ] }, { "dir": "lib" }, { "dir": "main-process", "nodes": [{ "dir": "menu", "nodes": [{ "file": "index", "ext": "ts" }] }, { "file": "main", "ext": "ts" } ] }, { "dir": "models" }, { "dir": "shared-process", "nodes": [{ "file": "index", "ext": "ts" }] }, { "dir": "ui", "nodes": [{ "file": "index", "ext": "tsx" }] } ] }, { "dir": "static", "nodes": [{ "dir": "common" }, { "dir": "logos" }, { "file": "error", "ext": "html", "data": true }, { "file": "index", "ext": "html", "data": true } ] }, { "dir": "styles", "nodes": [{ "dir": "mixins" }, { "dir": "ui", "nodes": [{ "file": "_app-menu-bar", "ext": "scss" }, { "file": "_focus", "ext": "scss" }, { "file": "_title-bar", "ext": "scss" } ] }, { "file": "_globals", "ext": "scss" }, { "file": "_mixins", "ext": "scss" }, { "file": "_type", "ext": "scss" }, { "file": "_ui", "ext": "scss" }, { "file": "_variables", "ext": "scss" }, { "file": "_vendor", "ext": "scss" }, { "file": "appname", "ext": "scss" } ] }, { "dir": "test" }, { "file": "package", "ext": "json", "data": true }, { "file": "webpack.common", "ext": "js" }, { "file": "webpack.development", "ext": "js" }, { "file": "webpack.production", "ext": "js" } ] }, { "dir": "docs", "nodes": [{ "dir": "contributing", "nodes": [{ "file": "setup", "ext": "md" }, { "file": "styleguide", "ext": "md" }, { "file": "tooling", "ext": "md" }, { "file": "troubleshooting", "ext": "md" } ] }, { "dir": "process", "nodes": [{ "file": "issue-triage", "ext": "md" }, { "file": "releasing-updates", "ext": "md" }, { "file": "reviews", "ext": "md" }, { "file": "roadmap", "ext": "md" } ] }, { "dir": "technical" }, { "file": "installation", "ext": "md" }, { "file": "README", "ext": "md" } ] }, { "dir": "script", "nodes": [{ "file": "build" }, { "file": "debug" }, { "file": "dist-info", "ext": "js" }, { "file": "package" }, { "file": "publish" }, { "file": "run", "ext": "js" }, { "file": "start" } ] }, { "file": ".gitmodules" }, { "file": ".travis", "ext": "yml" }, { "file": "appveyor", "ext": "yml" } ] scan('', json) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM