'How can I turn this array structure into this other pattern? [closed]

From this:

[["a", "b"], ["a", "c"], ["a", "d"], ["d", "e"], ["d", "f"], ["f", "g"]]

To this:

[["a", "b"], ["a", "c"], ["a", "d", "e"], ["a", "d", "f", "g"]]

The first element of each inner array represents a "father" and the second one a "son", so the final goal is to find grandsons, grandgrandsons... and then group them in bigger arrays, where all descendants are together.

Maybe it's an easy task, but I've been struggling to figure out how to get it!



Solution 1:[1]

If you like to get all relations, without parts which are included in other results, you could get an object of all descendant and start with the parents who are not having parents.

let
    getNodes = k => parents[k]
        ? parents[k].flatMap(getNodes).map(a => [k, ...a])
        : [[k]],
    data = [["a", "b"], ["a", "c"], ["a", "d"], ["d", "e"], ["d", "f"], ["f", "g"]],
    parents = {},
    children = new Set,
    result;


for (const [p, c] of data) {
    (parents[p] ??= []).push(c);
    children.add(c);
}

result = Object
    .keys(parents)
    .filter(p => !children.has(p))
    .flatMap(getNodes);

result.map(a => console.log(...a));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1