0

Eventually I am hoping to get an object to display like this:

'0': {
       '0': {
              label: 'Book:',
              content: 'a book name',        
       }, 

       '1': {
              label: 'Video:',
              content: 'a video name',        
       }, 

       '2': {
              label: 'Audio:',
              content: 'an audio name',        
       },              
  }, 

'1': {
       '0': {
              label: 'Book:',
              content: 'another book name',        
       }, 

       '1': {
              label: 'Video:',
              content: 'another video name',        
       }, 

       '2': {
              label: 'Audio:',
              content: 'another audio name',        
       },              
  }

My code in http://jsfiddle.net/ScbjL/4/ didn't turn out right, I'm puzzled why objDL is undefined?

2
  • It is undefined because objDT.label is undefined. you need to specify a key to select the correct object to pull the label from. Commented Feb 1, 2013 at 15:29
  • Also the next line has the same issue with objDD - looks like you're missing a subscript objDD[i]. Commented Feb 1, 2013 at 15:34

1 Answer 1

1

You should not iterate over .containers, dts and dds separately - you are forgetting from which container the objDT/DD is stemming. Also use an array instead of an object for the results.

var objDL = [];
$("dl").each(function(i) {
    objDL[i] = [];
    $(this).children("dt").each(function(j) {
        var $this = $(this);
        objDL[i][j] = {
            title: $this.text(),
            description: $this.next("dd").text();
        };
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

the i only loops twice, where there are three sets of dt/dd. Could it be the loop closure issue where I need to figure out how to maintain the index value of m & n in objDL loop?
Right, you want to group them by their .container, I missed that. Yet that would be very complicated since objDT and objDD did not distinguish that. See my snippet on how to do it without those
It works like a charm! Thanks for the help! is objDL[i][j] a multidimensional array?
@user1824996: Yeah, it's an array of arrays of objects - just the structure you described with plain objects.

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.