Reputation: 5654
I have an array with the structure
var citizens1 = [{lat:null,lng:null,socialSecurityNumber:null}];
I would like to use a loop to print the values of each element however this does not work for me:
Code
function animate(index,d) {
if (d>eol[index]) {
marker[index].setPosition(endLocation[index].latlng);
if(marker[index].getPosition() == endLocation[index].latlng){
console.log('Completed'+' '+index);
}
return;
}
var p = polyline[index].GetPointAtDistance(d);
marker[index].setPosition(p);
updatePoly(index,d);
timerHandle[index] = setTimeout("animate("+index+","+(d+step)+")", tick);
citizens1.push({lat:marker[index].getPosition().lat(),lng:marker[index].getPosition().lng(),socialSecurityNumber:global_citizens[index].socialSecurityNumber});
if(citizens1.length = '500'){
console.log('500 records saved');
window.clearTimeout( timerHandle);
for(var i = 0; i < citizens1.length ; i++){
console.log(citizens1.lat +',' +citizens1.lng+','+citizens1.socialSecurityNumber);
}
citizens1 = [];
}
}
Error
TypeError: citizens1[i].lat is undefined
Upvotes: 0
Views: 95
Reputation: 29
I hope you have initialised the Students as
Students = [{name:null,age:null,address:null}];
Upvotes: 1
Reputation: 707198
This line:
console.log(citizens1.lat +',' +citizens1.lng+','+citizens1.socialSecurityNumber);
needs to have the array index in it:
console.log(citizens1[i].lat +',' +citizens1[i].lng+','+citizens1[i].socialSecurityNumber);
Upvotes: 1
Reputation: 2287
citizens1.length = '500'
should be citizens1.length == 500
Also
console.log(citizens1.lat +',' +citizens1.lng+','+citizens1.socialSecurityNumber);
should be
console.log(citizens1[i].lat +',' +citizens1[i].lng+','+citizens1[i].socialSecurityNumber);
You need to print the properties of each element of the array, not of the array itself.
Upvotes: 1