【发布时间】:2020-01-13 04:59:00
【问题描述】:
我们可以使用 jq for bash (https://stedolan.github.io/jq/) 转换下面的示例吗?
要求是将文件路径转换为json,如下例所示
const data = [
"/parent/child1/grandchild1"
"/parent/child1/grandchild2"
"/parent/child2/grandchild1"
];
const output = {};
let current;
for (const path of data) {
current = output;
for (const segment of path.split('/')) {
if (segment !== '') {
if (!(segment in current)) {
current[segment] = {};
}
current = current[segment];
}
}
}
console.log(output);
【问题讨论】:
-
你试过什么代码?
标签: json bash jq directory-structure pathname