2

I'd like to combine a set of $.each statements looping through an array of arrays containing objects of k:v pairs into a single $.each which I can increment.

Structure of array returned from PHP: an array of arrays containing objects.

enter image description here

PHP: this is what is run then sent back to the JavaScript.

array_push($allDataArray, $completedLevelData, $completedQuestData, $completedNPCData);

JavaScript:

I was originally using a separate $.each to loop through each array of objects as such. But I would like to consolidate these $.each statements into something smaller:

function updateData (functionToRun, callback) {  
   $.getJSON("phpscripts.php", {
            "_player" : Player,
            "_playerID" : UserID,
            "_functionToRun" : functionToRun
        },

        function(result) {      
            var questIDs = [];

            $.each(result[0], function (i, allData) {   
                $(".completedLevels").append("<br/>"+allData['levelname']);
            });
            $.each(result[1], function (i, allData) {   
                questIDs.push(allData['quest_id']);
                $(".completedQuests").append("<br/>"+allData['quest_id']+ ", " + allData['questname']);
            });
            $.each(result[2], function (i, allData) {   
                $(".completedNPCs").append("<br/>"+allData['npc_name']);
            });
etc...      

To do this, I was just going to loop through with a for and append... but the for, unlike each, keeps looping and adding elements even when they're null. For example,

for (obj in result) {
    for (obj2 in result[obj]) {
        console.log(result[obj][obj2]['levelname']);
    }
}

Gives:

mymap2 
mymap 
(9) undefined 
[] 

So it continues looping even if there is no data. I don't want it to do that.

That being said, I then tried a $.each, and tried incrementing the parent array when it has looped through all the child arrays. Essentially mimicking above, but combining all $.each into one $.each, like so:

var k = 0, j = 0;
$.each(result[k], function (i, allData) {   
    //if you loop through all objects within parent object, move to next parent object
    if (j == result[k].length) {
        k++;
        j=0;
    }
    j++;
 etc...

But it just stops once it has looped through all of result[0], without incrementing k. Any idea how to do this?


Edit: I tried the answer below, but it still shows undefineds as being loaded:

for (var i = 0; i < result.length; i++) {
    $.each(result[i], function (j, allData) {
        $(".completedLevels").append("<br/>"+allData['levelname']);
    });
}

The following is appended:

Completed levels:

  • mymap
  • undefined
  • undefined
  • undefined
user3871
  • 12,432
  • 33
  • 128
  • 268
  • Is there any chance you can perform a JSON stringify on your data and place the result in your question? It will allow me to test using the same dataset. – Nick Coad Jun 04 '14 at 03:00
  • It looks like simpyl the arrays in `result[1]` and `result[2]` don't contain objects with `.levelname`s, but with `.quest_id`s and `.npc_name`s. No wonder you get `undefined` when trying to access `levelname` there. Btw, [don't use `for in` loops on arrays](http://stackoverflow.com/q/500504/1048572). – Bergi Jun 04 '14 at 04:30

1 Answers1

0

You're not actually using any JavaScript to tell the browser that you want to run the "$.each" command after it's completed once.

Pop it inside a for loop:

for (var i = 0; i < result.length; i++) {
    $.each(result[i], function (j, allData) {   

        // Do whatever you like in here            

    }
}

Fiddle: http://jsfiddle.net/9fhj4/

Edit: For the record, a better approach for what you're doing, if you're just dealing with arrays, would be to use for in both statements:

for (var i = 0; i < result.length; i++) {
    for (var j = 0; j < result[i].length, j++) {   

        // Do whatever you like in here      

    }
}

Edit again: in response to your comment, I've tried modifying the fiddle to more accurately represent the data structure you're working with: http://jsfiddle.net/9fhj4/2/

Nick Coad
  • 3,623
  • 4
  • 30
  • 63
  • I would need three for loops because it's an array of arrays containing objects. Anyway, why is it better to use for loops only? – user3871 Jun 04 '14 at 02:39
  • @Growler see above, I've modified my answer to include an example that bears a stronger resemblance to your actual dataset. – Nick Coad Jun 04 '14 at 03:09
  • Regarding your question about why you should use for instead of each see this question: http://stackoverflow.com/questions/11887450/each-vs-for-loop-and-performance – Nick Coad Jun 04 '14 at 03:10