1

Lets say I have an unordered nested list:

<ul>
   <li>Item a1</li>
   <li>Item a2</li>
   <li>Item a3</li>
       <ul>
           <li>Item b1</li>
           <li>Item b2</li>
           <li>Item b3</li>
            <ul>
               <li>Item c1</li>
               <li>Item c2</li>
               <li>Item c3</li>             
            </ul>
           <li>Item b4</li>
       </ul>
  <li>Item a4</li>
</ul>

I need to traverse it and save it in a two dimensional array (ultimately I'm just trying to convert it into a JSON entity) I am allowed to use both Jquery AND/OR Javascript. How should I proceed?

Thanks

2
  • Looking at that list you look like you have a three dimensional data structure at least (three nested lists). Could you clarify what you want your output array to look like? Also I don't think you can nest <ul> directly inside another <ul>. I assume it should be inside the <li> immediately preceding it... Commented Jul 1, 2010 at 13:37
  • Thank you chris, I need a JSON entity as my output. Moreover, I will also be okay with two nested lists. Thank you Commented Jul 1, 2010 at 14:00

3 Answers 3

2

I'm not sure exactly what you want the resulting data structure to look like, but this (which uses some jQuery):

$(function() {

    var result = {};

    function createNewLevel(parent,items) {
        var length = items.length;
        for(var i = 0; i < length; i++) {
            if(items[i].tagName == 'UL') {
                parent['ul' + i] = {};
                createNewLevel(parent['ul' + i],$(items[i]).children().get());
            } else {
                parent['li' + i] = $(items[i]).text();
            }
        }
    }

    createNewLevel(result, $('ul:first').get());

    console.log(result);

});

... would produce this structure

{
    ul0: {
        li0: "Item a1",
        li1: "Item a2",
        li2: "Item a3",
        li4: "Item a4",
        ul3: {
            li0: "Item b1",
            li1: "Item b2",
            li2: "Item b3",
            li4: "Item b4",
            ul3: {
                li0: "Item c1",
                li1: "Item c2",
                li2: "Item c3"
            }
        }
    }
}

It could be fairly easily tweaked to alter details of the resulting structure if needed.

Please note that this is a javascript object. If you actually need a JSON object, then you just need to convert it using var jsonResult = JSON.stringify( result );

Sign up to request clarification or add additional context in comments.

1 Comment

I find it odd when people mark an answer as the correct answer, but don't upvote it.
2
function traversing(ul)
{
     for(var index=0;index<ul.childNodes.length;index++){
          if(ul.childNodes[index].childNodes.length>0){
              traversing(ul.childNodes[index]);
          }
          //perform other operation
     }
}

3 Comments

@Chris - Oh sorry, you're so right! A stupid mistake regarding the nesting of ul's (will edit it now). Regarding with my output, I will need it to be like : { name:item a1 children:[], name:item a2 children:[], name:item a3 children:[{ name:item b1 children:[], name:item b2 children:[], name:item b3 children:[{ name:item c1 children:[] }], }] }
rather: { {name:item a1 children:[]}, {name:item a2 children:[]}, {name:item a3 children:[ {name:item b1 children:[]}, {name:item b2 children:[]}, {name:item b3 children:[ {name:item c1 children:[]} ]}, ]} }
@Galambalazs Yes, yes...thats it (sorry for the illegible code) you have any code to format my output like that?
0

With JQuery try:

$.each($("li"), function(index, value) { 
  alert(value); 
});

Comments

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.