0

I have a bit of a puzzle on my hands.

I have 2 $.each blocks that get data from a JSON file, but I need them to one after another instead of one block first and then the next one. I know it is in my logic, but I can't seem to crack this one :-(

                //block 1
                if (entry.hasOwnProperty('class2')) 
                {   
                    $.each(entry.class2, function (index, data) 
                    {
                        test01 = this.name;
                    });

                }

                //block 2
                if (entry.hasOwnProperty('tutors')) 
                {   
                    $.each(tutors, function (index, data) 
                    {
                        test02 = this.fname;
                    });

                }

So these both have multiple entries and what it is doing at the moment is:

block 1, block 1, block1, block 2, block 2, block 2 // according to the amount of entries.

What i need is:

block 1, block 2, block 1, block 2, block 1, block 2 // according to the amount of entries.

I have tried using functions, but no success there, but this logic is inside another $.each block and yes they have to be inside that $.each block.

jwknz
  • 6,598
  • 16
  • 72
  • 115

1 Answers1

0

I'm assuming you're using the same data structure as in Accessing second array in a JSON decode using Jquery

Use a single $.each, making use of the index to refer to the corresponding element in the other array:

var class2 = raw00.allData[0];
var tutor = raw00.allData[1];

$.each(class2, function(index, data) {
    console.log(this.name);
    var that = tutor[index];
    console.log(that.fname);
});
Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for you help again ;-) but does entry.tutors not look with entry.class2 - I get this error: entry.tutors is undefined – jwknz Nov 17 '14 at 00:46
  • You need to show the data. I remember it from your last question, but it won't be clear to anyone else reading this question. – Barmar Nov 17 '14 at 00:50
  • I had to make a slight adjustment, but it worked :-) Thank you. `var class2 = msg.allData[0].class2;` `var tutor = msg.allData[1].tutor;` – jwknz Nov 17 '14 at 01:08