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!