Ugran
Ugran

Reputation: 25

Recursive loop in Javascript

I try to write certain attributes to html with recursive loop, but can't make the code work :(

I have array of hashes as you can see in Json. With following attributes: serno - serial number, parent_serno - serial number of parent, name - name of the attribute. I want to first write down every hash.name that has "parent_serno == 0" and then after each one I want to write name of hashes that have "parent_serno = serno(of first hash)" it's kind of grouping them up according to serno and parent_serno.

Can you guys tell me what I do wrong?

 var dataBase = [{"serno": 1, "parent_serno": 0, "name": "Home"},
            {"serno": 2, "parent_serno": 0, "name": "Search"},
                {"serno": 10, "parent_serno": 2, "name": "Search Payment"},
            {"serno": 11, "parent_serno": 2, "name": "Problematic Search Payment"},
            {"serno": 12, "parent_serno": 2, "name": "Cash Error"},
            {"serno": 13, "parent_serno": 2, "name": "Payment Note"},
            {"serno": 89, "parent_serno": 2, "name": "Search Payment By Category"},
            {"serno": 131, "parent_serno": 2, "name": "Search Payment New"},
            {"serno": 3, "parent_serno": 0, "name": "User Mangement"},
            {"serno": 4, "parent_serno": 0, "name": "Service Provider"},
            {"serno": 5, "parent_serno": 0, "name": "General"},
            {"serno": 88, "parent_serno": 5, "name": "Balance and Financial"},
            {"serno": 14, "parent_serno": 5, "name": "My Subagents"},
            {"serno": 15, "parent_serno": 5, "name": "My Providers"},
            {"serno": 16, "parent_serno": 5, "name": "My Dealers"},
            {"serno": 17, "parent_serno": 5, "name": "My Wallets"},
            {"serno": 19, "parent_serno": 5, "name": "Accounts"},
            {"serno": 45, "parent_serno": 19, "name": "Bank Accounts"},
            {"serno": 46, "parent_serno": 19, "name": "Transfers"},
            {"serno": 0, "parent_serno": 5, "name": "My Statements"},
            {"serno": 47, "parent_serno": 20, "name": "My Terminals"}];

        var funkcia = function(parent) {
            for (var i=0, i < dataBase.length, i++){
                if (dataBase[i].parent_serno == parent){
                    document.write(dataBase[i].name);
                    parent = dataBase[i].serno;
                    funkcia(parent);
                };
                  };
                    };

funkcia(0);

Upvotes: 0

Views: 95

Answers (1)

Bergi
Bergi

Reputation: 664548

 parent = dataBase[i].serno;
 funkcia(parent);

This is your problem. You're altering the parent variable, but then let the for-loop run on, now searching for the wrong parent.

Either you use a different variable:

function funkcia(parent) {
    for (var i=0; i < dataBase.length; i++){
        if (dataBase[i].parent_serno == parent){
            document.write(dataBase[i].name);
            var new_parent = dataBase[i].serno;
            funkcia(new_parent);
        }
    }
}

or none at all:

function funkcia(parent) {
    for (var i=0; i < dataBase.length; i++){
        if (dataBase[i].parent_serno == parent){
            document.write(dataBase[i].name);
            funkcia(dataBase[i].serno);
        }
    }
}

Upvotes: 1

Related Questions