var tree2 = {
name: "page.js",
require: [
{
name: "A.js",
require: [
{
name: "B.js",
require: [
{
name: "C.js",
require: [
{
name: "E.js"
}
]
},
],
},
],
},
{
name: "D.js",
require: [
{
name: "C.js",
require: [
{
name: "E.js"
}
]
},
{
name: "E.js",
},
],
}
],
};
function resolve(node) {
const totalDeps = [];
const { name, require = [] } = node;
if (require.length === 0) {
return [name];
}
for (const requireNode of require) {
let deps = resolve(requireNode);
deps = deps.filter(dep => !totalDeps.includes(dep));
totalDeps.push(...deps);
}
totalDeps.push(name);
return totalDeps;
}
console.log(resolve(tree2));
console