-2

I am working on an angular application. My data is as follows

  data= [
        {
            "id": 2,
            "name": "Jamie",
            "objectId": 200,
            "parentId": null,
            "children": [
                {
                    "id": 98,
                    "name": "Rose",
                    "objectId": 100,
                    "parentId": 200,
                    "children": [
                        {
                            "id": 1212,
                            "name": "julie",
                            "objectId": 500,
                            "parentId": 100,
                            "children": []
                        }
                    ]
                },
                {
                    "id": 67,
                    "name": "Kosy",
                    "objectId": 700,
                    "parentId": 200,
                    "children": []
                }
            ]
        }
    ]

I will be having input id and name. Suppose in my method I get id as 1212 and name as "julie". So I want to go to that node whose id is 1212 and name is equal to "julie", once this condition is met. I need to check parentId in children is equal to objectId in parent till parentId becomes null. If parent id becomes null then it is considered as last node and then I want to have my data in the array in following format. For id 1212 and name "julie" resultArray is resultArray = ["Jamie/Rose/Julie "]. Data starting from parent to children separated by slash.

Another example is if I get id as 67 and name "Kosy". then result array will be

resultArray = ["Jamie/Kosy"]

As parentId of Kosy is 200 and ObjectId of Jamie is 200, which indicate that Jamie is parent of Kosy, that's why data should look like this. want to have dynamic code as at run time data can be huge but structure and logic will be same as mentioned above

How can I do this

Julie
  • 87
  • 5
  • Does this answer your question? [Javascript - Find path to object reference in nested object](https://stackoverflow.com/questions/43636000/javascript-find-path-to-object-reference-in-nested-object) Use [Find by key deep in a nested object](https://stackoverflow.com/q/15523514/215552) to find the object. – Heretic Monkey May 20 '21 at 16:08
  • nops I think.... – Julie May 20 '21 at 16:09
  • You have a question with XY problem. What you are doing is a known algorithm. Look for that. Something like https://stackoverflow.com/q/9133500/5468463 – Vega May 20 '21 at 16:30
  • I don't know about that could you please help providing a solution? – Julie May 20 '21 at 16:31

1 Answers1

0

I wrote the code for this problem. Please try the following code. This is one of the typical tree-search problem. One point what sets it apart from traditional tree search is checking its parent. That was easily solved here.

const data = [
    {
        "id": 2,
        "name": "Jamie",
        "objectId": 200,
        "parentId": null,
        "children": [
            {
                "id": 98,
                "name": "Rose",
                "objectId": 100,
                "parentId": 200,
                "children": [
                    {
                        "id": 1212,
                        "name": "julie",
                        "objectId": 500,
                        "parentId": 100,
                        "children": []
                    }
                ]
            },
            {
                "id": 67,
                "name": "Kosy",
                "objectId": 700,
                "parentId": 200,
                "children": []
            }
        ]
    }
];

// function that search the target in a node(not a array)
// In order for this function to return true, id comparison from the final node to this node should be passed as well
const findInNode = (node, id, name, output) => {
    if (node.name === name && node.id === id) { // self test
        return true;
    } else {
        const children = node.children;
        if (!children) return false;
        // find in children
        for (let child of children) {
            // output.paths is the current search path
            output.paths.push(child.name);
            if (findInNode(child, id, name, output)) {
                // if found, compare the self objectId and child's parentId
                return child.parentId === node.objectId;
            }
            output.paths.pop();
        }
    }
}

// function that search the target in an array, for use at the top-most level
const findInArray = (nodes, id, name, output) => {
    for (let node of nodes) {
        output.paths.push(node.name);
        if (findInNode(node, id, name, output)) {
            if (!node.parentId) {
                output.found = true;
                break;
            }
        }
        output.paths.pop();
    }
}

output = { paths: [], found: false };
findInArray(data, 1212, 'julie', output);

console.log(output.found);
console.log(output.paths.join('/'));
TopW3
  • 1,477
  • 1
  • 8
  • 14
  • hey could you please edit your answer so that I can run it here on stackoverflow only – Julie May 20 '21 at 16:32
  • and what is this output object used for? could you also please add comments to your code so that I can understand it? – Julie May 20 '21 at 16:34
  • output.path is the path array to the found node, and output.found is the flag if the node was found – TopW3 May 20 '21 at 16:36
  • could you please add comments on your code, I can see it is giving correct output but I am not able to understand much how it is working – Julie May 20 '21 at 16:41
  • thanks just last question. what is the need of output.pop() . Will code go in that part? – Julie May 20 '21 at 16:59
  • yes. pop is needed to go to the original path if a child is failed(pop this child) – TopW3 May 20 '21 at 17:00