Hey everyone, I have a JSON array that pulls data, its an immense amount of data that i use. It is a multilevel array eg:

dateTime: 2018-12-04T10:30:45:222z
comments:""
+Driver[0]

    [0]
        fname: John
            lname: Doe
        driverCode: DOEJ

    [1]
        fname: Mary
        lname: Smith
        driverCode: SMIMA
more info etc

I am trying to access dateTime, as well as driver.fname, driver.driverCode etc...

here is what i have tried so far...

var json = results;
      var li;

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

        var stTime = new Date(json[i].dateTime);
        var status = json[i].status;
        var id = json[i].id;
        var driverf = json[i].driver[j].firstName;
        var driverl = json[i].driver[j].lastName;
        var driverCode = json[i].driver[j].employeeNo;
        var origin = json[i].origin;
        var editTime = new Date(json[i].editDateTime);

Its telling me that I am missing a ")" on for loop for "j" and it cannot find the object of length on "j" loop. What am i doing wrong? This seems like it is legit, but I am not sure.

For starters, the structure in your json object seems odd. Let me re-format it, so that we can give it a closer look:

[{
    dateTime: "2018-12-04T10:30:45:222z"
    comments:""
    drivers: [
        {
            fname: 'John',
            lname: 'Doe',
            driverCode: 'DOEJ'
        },
        {
            fname: 'Mary',
            lname: 'Smith',
            driverCode: 'SMIMA'
        }
    ]
},]

That's better.

Now, the for sintax is invalid in both cases. The for syntax allows for three semi-colon separated rules, whereas you have four. You separated the declaration and initialization of the index variables. The correct syntax is for (var i = 0; i < json.length; i++).

Additionally, you can iterate the array using a for..in. So instead of for(var i = 0; i < json.length; i++) you can iterate the array like so:

for(var i in jsonArray){
    var objectInstance = jsonArray[i];
    for(var j in objectInstance.drivers) {
        var driver = objectInstance.drivers[j];
        ...
    }
}

Also, by instancing a variable with the specific index of the json object you're calling (var driver = objectInstance.drivers[j] in this example), you save yourself from bidimensional array hell, avoiding using i and j as much as possible.

Hope this helps!

commented: great answer and +1 for specifying the reason for editing this ( I never do it but I should ) +9

Hi, thanks for the reply,

i updated my code as follows:

var jsonArray = results;
            var li;
             for(var i in jsonArray){
                    var objectInstance = jsonArray[i];
                    for(var j in objectInstance.drivers) {
                        var driver = objectInstance.drivers[j];

               var stTime = new Date(results[i].dateTime);
               var status = objectInstance[i].status;
               var id = objectInstance[i].id;
               var driverf = driver.firstName;
               var driverl = driver.lastName;
               var driverCode = driver.employeeNo;
               var origin = objectInstance[i].origin;
               var editTime = new Date(objectInstance[i].editDateTime);
               console.log(driverf);
               console.log(status);
               let timeDifferenceMilliseconds = (new Date().getTime() - stTime.getTime());

               let timeDifferenceHours = timeDifferenceMilliseconds / 3600000;

               let timeDifferenceEditTime = (editTime.getTime() - stTime.getTime());

               let editDifferenceHrs = timeDifferenceEditTime / 3600000; 

               //console.log("Driver "+driverf+" Created Time: "+ stTime +"Edited :"+ editTime+ " Hours: "+ Math.round(editDifferenceHrs))

               if((timeDifferenceHours) > 30 && editDifferenceHrs >24){
                   li = $('<li/>');
                   li.append("Driver "+ driverf +" "+  driverl+" ("+ driverCode +")" +" on "+ stTime + " Has The Status Of " + status +" and is greater than "+Math.round(timeDifferenceHours) +" hours");
                   $('#listDrivers').append(li);
                   refreshOnDutyReport();
               }
             }
           }

but now it wont even console.log anything. I think I am doing this right, since i have never used a for (in) loop

This is what is returned

You probably want to be using a for...of loop here instead of a for...in, which means you don't need to set the variable using the index every time.

const drivers = [
    {
        dateTime: "2018-12-04T10:30:45:222z",
        comments: "",
        drivers: [
            {
                fname: "John",
                lname: "Doe",
                driverCode: "DOEJ"
            },
            {
                fname: "Mary",
                lname: "Smith",
                driverCode: "SMIMA"
            }
        ]
    }
];

for (let d of drivers) {
    console.debug(d);
};

Lastly, don't build stuff in the DOM by appending strings. It's slow, ugly and error prone. Use a templating library instead.

I see your Json structure is pretty complex. Consider adding breakpoints to your code in the JS debugger on your browser and accessing the properties from every nested loop's scope. Then, replicate the calls in your code. Wash, rinse, repeat.

Hope that helps!