我需要JavaScript才能从数组中获取最深的路径。
输入是路径数组 预期的输出:仅最深的路径
[
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\completion-provider.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export\\global.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export\\index.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export\\local.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\index.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\babel.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\global.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\index.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\local.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\webpack.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\main.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\package-configs.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\filter-lookups-by-text.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\get-exports-from-prefix.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\get-module-from-prefix.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\internal-modules.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\lookup-alias.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\lookup-commonjs.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\path-helpers.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\regex-patterns.js"
]
这是另一个输入示例
[
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\completion-provider.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export\\global.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export\\index.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export\\local.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\index.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\babel.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\global.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\index.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\local.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\webpack.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\main.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\package-configs.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\filter-lookups-by-text.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\get-exports-from-prefix.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\get-module-from-prefix.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\internal-modules.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\lookup-alias.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\lookup-commonjs.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\path-helpers.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\regex-patterns.js",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\package-lock.json",
"D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\package.json"
]
答案 0 :(得分:1)
您可以将路径字符串转换为具有父项和子项的项目:
const createItem = (path, parent) => ({
children: [],
path,
parent,
});
然后对路径字符串进行排序,以使所有其他子级的根路径成为第一个:
const sortedPaths = paths.slice().sort();
然后将排序后的路径字符串简化为包含所有项目,但将childs属性填充为其实际子项的Map:
sortedPaths
.filter((path)=>!path.endsWith('.js'))//filter out js files
.map((path) => [path, parentPath(path)])
.slice(1) //remove first, it is root
.reduce((map, [path, parentPath]) => {
const parent = getParent(map, parentPath);
const item = createItem(path, parent);
parent.children.push(item);
return map.set(path, item);
}, new Map().set(sortedPaths[0], createItem(sortedPaths[0])))
尽管您只需要rootNode,所以您可以在该地图上执行.get(sortedPaths[0])
。
如果您将根节点传递给名为getLastDescendents
的函数,则此函数可以递归检查子项并仅添加没有子项的项。因为您有嵌套的项目,它将产生一个需要展平的多维数组:
const flatten = (arr) =>
arr.reduce(
(result, item) =>
!Array.isArray(item)
? result.concat(item)
: result.concat(flatten(item)),
[],
);
const getLastDescendents = (rootNode) => {
const recur = (result, node) =>
node.children.length === 0
? result.concat(node)
: result.concat(
node.children.map((node) => recur(result, node)),
);
return flatten(recur([], rootNode));
};
总共看起来像这样:
const paths = [
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\completion-provider.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export\\global.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export\\index.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\export\\local.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\index.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\babel.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\global.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\index.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\local.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\lookups\\module\\webpack.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\main.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\package-configs.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\filter-lookups-by-text.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\get-exports-from-prefix.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\get-module-from-prefix.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\internal-modules.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\lookup-alias.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\lookup-commonjs.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\path-helpers.js',
'D:\\Program Files\\atom\\.atom\\packages\\autocomplete-modules\\lib\\utils\\regex-patterns.js',
];
const sortedPaths = paths.slice().sort();
const parentPath = (path) =>
path
.split('\\')
.slice(0, -1)
.join('\\');
const getParent = (map, path) =>
map.get(path) || getParent(map, parentPath(path));
const createItem = (path, parent) => ({
children: [],
path,
parent,
});
const rootNode = sortedPaths
.filter((path)=>!path.endsWith('.js'))//filter out js files
.map((path) => [path, parentPath(path)])
.slice(1) //remove first, it is root
.reduce((map, [path, parentPath]) => {
const parent = getParent(map, parentPath);
const item = createItem(path, parent);
parent.children.push(item);
return map.set(path, item);
}, new Map().set(sortedPaths[0], createItem(sortedPaths[0])))
.get(sortedPaths[0]);
const flatten = (arr) =>
arr.reduce(
(result, item) =>
!Array.isArray(item)
? result.concat(item)
: result.concat(flatten(item)),
[],
);
const getLastDescendents = (rootNode) => {
const recur = (result, node) =>
node.children.length === 0
? result.concat(node)
: result.concat(
node.children.map((node) => recur(result, node)),
);
return flatten(recur([], rootNode));
};
console.log(
getLastDescendents(rootNode).map((node) => node.path),
);