0

I am trying to list out all the elements inside an array and as you can see Company has three levels, but I've only written the script to print the output until two levels. How do I access the third level? What should be the array that I should be using inside the third for loop?

4
  • 2
    We need to go deeper Commented Jul 13, 2012 at 4:31
  • Im confused about the array name that I should be using in the third level.. It gets a bit long n dirty when I get in to the third level Commented Jul 13, 2012 at 4:31
  • @dda: I haven't seen any question for about 2 hours that you haven't beautified. Nice job! ;-) Commented Jul 13, 2012 at 4:33
  • Which means we look at the same questions. I ignore plenty :-) Commented Jul 13, 2012 at 4:33

2 Answers 2

2

What you're looking for is recursion.

Here is a fixed version of your fiddle: http://jsfiddle.net/jEmf9/

function generateEntity(obj) {
    var html = [];

    var name = obj.entity;
    html.push('<li>');
    html.push(name);
    html.push('</li>');
    var arrayName = name.replace(/\s/gi, '_');
    if (obj[arrayName] == undefined) {
        return html.join('');
    }

    var entity = obj[arrayName];
    for (var i = 0; i < entity.length; i++) {
        html.push('<ul>');
        html.push(generateEntity(entity[i]));
        html.push('</ul>');
    }
    return html.join('');
}
Sign up to request clarification or add additional context in comments.

3 Comments

Interesting - a 49 minute-old question and the first two answers come in 8 seconds apart. :) +1 to you for the cleaner version.
Funny. I would have finished it a couple minutes ago, but I just couldn't leave that += code in. Probably a touch of OCD.
Thanks for the answer..I knew how to do this recursively... bt I wanted to know if I had fixed depth of three.. How am I gonna use the for loop.. How am I gonna access the third level of entities?
0

In your case you do not need a special technique for accessing the third level. You need to write a recursive tree walking function so that you can render a tree of any depth.

I've done a quick patch of your code here: http://jsfiddle.net/rtoal/xcEa9/6/

Once you get things working as you like, you can work on forming your html. Your repeated string concatenation using += is known to be extremely inefficient, but that is outside the scope of this question. :)

2 Comments

Thanks for the answer..I knew how to do this recursively... bt I wanted to know if I had fixed depth of three.. How am I gonna use the for loop.. How am I gonna access the third level of entities?
thanks.. I know its not recomended... I wanted to know how it can be done If I went this way.. I have used recursion in the proper program.. bt this question was out of curiousity..

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.