How to do a recursive search for this Javascript / JSON tree?

I am writing Javascript code. I have a JSON object that looks like this (spacing changed for readability):

var myNodes = [
    {
        id: 4, 
        children: [
            {
                id: 1, 
                children: [
                ]
            },
            {
                id: 3, 
                children: [
                ]
            }
        ]
    },
    {
        id: 2, 
        children: [
        ]
    },
    {
        id: 6, 
        children: [
            {
                id: 5, 
                children: [
                ]
            }
        ]
    }
]

Each node id in this data structure is unique. However, other than this, there are no fixed or known relationships between ID numbers in general. And the number of children at each level is not limited.

If I want to find this tree and return node with id == 6. How can I do this? I wrote the following code, however, after it encounters the first node leaf, the whole algorithm returns false, which is clearly wrong. I'm just trying to do a basic depth search. But I do not want to add additional fields to this data and “mark” the nodes, as I saw in some DFS implementations on the Internet.

myClass.prototype.nodeSearch = function nodeSearch(treeNodes, searchID){
    for (var nodeIdx = 0; nodeIdx <= treeNodes.length-1; nodeIdx++)
    {
        console.log("Comparing treeNodes element with ID==" + treeNodes[nodeIdx].id + " to SearchID==" + searchID);
        if (treeNodes[nodeIdx].id == searchID)
        {    
            console.log("Match!");
            return treeNodes[nodeIdx];
        }
        else
        {
            console.log("No Match! Trying " + treeNodes[nodeIdx].children.length + " Children of Node ID#" + treeNodes[nodeIdx].id);
            return this.nodeSearch(treeNodes[nodeIdx].children, searchID); 
        }
    }
    console.log("Done trying " + treeNodes.length + " children. Returning False");
    return false;
};
+4
3

. if, else , , a return , ( 4 1 ). , , . , :

myClass.prototype.nodeSearch = function nodeSearch(treeNodes, searchID){
    for (var nodeIdx = 0; nodeIdx <= treeNodes.length-1; nodeIdx++) {
        var currentNode = treeNodes[nodeIdx],
            currentId = currentNode.id,
            currentChildren = currentNode.children;
        console.log("Comparing treeNodes element with ID==" + 
                    currentId + " to SearchID==" + searchID);
        if (currentId == searchID) {    
            console.log("Match!");
            return currentNode;
        }
        else {
            console.log("No Match! Trying " + currentChildren.length + 
                        " Children of Node ID#" + currentId);
            var foundDescendant = this.nodeSearch(currentChildren, searchID); 
            if (foundDescendant) {
                return foundDescendant;
            }
        }
    }
    console.log("Done trying " + treeNodes.length + " children. Returning False");
    return false;
};

, DRY er , .

+3

You can use DefiantJS ( https://github.com/hbi99/defiant.js ). Using this js-lib, you can search for JSON structures with XPath requests. And it's as simple as the code example below. Here is the same code in JSFiddle: http://jsfiddle.net/hbi99/a55au8h5/

PS: I added a name property for this sample code.

var myNodes = [
    {
        "id": 4,
        "name": 'name_of_4',
        "children": [
            {
                "id": 1,
                "name": 'name_of_1',
                "children": []
            },
            {
                "id": 3,
                "name": 'name_of_3',
                "children": []
            }
        ]
    },
    {
        "id": 2,
        "name": 'name_of_2',
        "children": []
    },
    {
        "id": 6,
        "name": 'name_of_6',
        "children": [
            {
                "id": 5,
                "name": 'name_of_5',
                "children": []
            }
        ]
    }
],
found = JSON.search(myNodes, '//*[id=6]');

console.log (found [0] .name); // name _of_6

0
source

Source: https://habr.com/ru/post/1539227/


All Articles