0

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);
1
  • 1
    Don't use semicolons ; after closing brackets } when closing a block... Commented Jul 2, 2014 at 10:37

1 Answer 1

1
 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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the comment, but putting break doesn't seem to solve the problem :(
Right, of course not. Fixed now. But notice that this will still not work as expected, because your dataBase does a have a circular relation between My Statements and General.
Yeah I guess I mistyped it. my statements should have serno = 0;
You might want to update your question with a corrected database then.
Home | Search | Search Payment | Problematic Search Payment | Cash Error | Payment Note | Search Payment By Category | Search Payment New | User Mangement | Service Provider | General | My Wallets | Accounts | Bank Accounts | Transfers | My Statements | Home | Search | Search Payment | Problematic Search Payment | Cash Error | Payment Note | Search Payment By Category | Search Payment New | User Mangement | Service Provider | General | My Wallets | Accounts | Bank Accounts | Transfers | My Statements. I get this output... it is still buggy, but its closer :) thank you for the answer bergi.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.