我创建了一个脚本,它接收一个字符串数组并过滤一个重复的免费单个字符串。
答案 0 :(得分:0)
const a = [
"Leetmeme: Cyberportal",
"Cyberportal: Ice,Fraudstream",
"CamelCaser: KittenService",
"Fraudstream: Leetmeme",
"KittenService:",
"Ice:"
];
//transforming `a` into a reasonable structure
let identity = v => v;
let dependencies = a.reduce((acc, str) => {
let [key, ...deps] = str.split(/[:,]\s*/g).filter(identity);
acc[key] = deps;
return acc
}, {});
let depth = 0; //just for logging
let status = {};
let load = (acc, module) => {
if(status[module] === "loading"){
var circle = Object.keys(status).filter(k => status[k] === "loading").concat(module);
throw new Error("circular reference found: " + circle);
}
if(status[module] !== "done"){
console.log(" ".repeat(depth++), module);
status[module] = "loading";
let deps = dependencies[module] || [];
deps.reduce(load, acc).push(module);
status[module] = "done";
--depth;
}
return acc;
}
let order = Object.keys(dependencies).reduce(load, []);
console.log(order);