SOURCE

function getAllRes(arrays) {
    if (!arrays || !arrays.length) {
        return [];
    }
    const startingArrays = [];
    // 找到所有作为起点的node
    for (let i = 0; i < arrays.length; i++) {
        const item = arrays[i];
        const index = arrays.findIndex((e) => {
            return e.destination === item.origin;
        });
        if (index === -1) {
            startingArrays.push(item);
        }
        // 遍历出所有组合
    }
    const A1 = [];
    for (let i = 0; i < startingArrays.length; i++) {
        const B1 = [[startingArrays[i]]];
        for (let j = 0; j < B1.length; j++) {
            const C1 = B1[j];
            for (let k = C1.length - 1; k < C1.length; k++) {
                const elements = arrays.filter((e) => {
                    return C1[k].destination === e.origin;
                });
                if (elements.length) {
                    elements.forEach((e, i) => {
                        if (i === 0) {
                            C1.push(e);
                        } else {
                            B1.push([...C1.slice(0,-1), e]);
                        }
                    });
                }
            }
            B1[j] = C1;
        }
        A1.push(...B1);
    }
    return A1;
}
let testArr = [
    { origin: 1, destination: 3 },
    { origin: 2, destination: 3 },
    { origin: 3, destination: 4 },
    { origin: 4, destination: 5 },
    { origin: 4, destination: 6 },
    { origin: 6, destination: 7 },
    { origin: 6, destination: 9 },
    { origin: 8, destination: 9 },
    { origin: 9, destination: 10 },
    { origin: 10, destination: 11 },
    { origin: 10, destination: 12 }
]
console.log(getAllRes(testArr))
console 命令行工具 X clear

                    
>
console