I have the weirdest problem with my code and I don't know how to solve it. I have an array
of objects
which I would like to loop through and match the variable dossiernummer
to the selected dossiernummer
to extract the zipcode
of the object. This is all a bit too much for one question and I am certain I can code this, but my array seems empty whenever I add a loop!
My code:
$('select[name=company]').on('change', function() {
// The selected `dossiernummer`
console.log($(this).val());
// The array of `Objects`
console.log(results);
geocodeAddress(geocoder, map, address);
});
When using the Chrome console, I can see the filled array of objects, so I am 100% sure that the data is there! I also checked the variables, and it all matches:
But! Now I would like to loop through the array of objects:
$('select[name=company]').on('change', function() {
for (var j = 0; j < results.length; j++){
if (results[j]['dossiernummer'] == $(this).val()){
var address = results[j]['postcode']
}
}
geocodeAddress(geocoder, map, address);
});
This gives the following error in my console:
I am not advanced with JavaScript, so this error is probably very silly but I can't figure it out. Help is much appriciated!
Edit:
The data is coming from an API to fill a Select2
params.page = params.page || 1;
var results = [];
if(data.totalItemCount > 0){
$.each(data._embedded.rechtspersoon, function(i, v) {
v.id = v.dossiernummer ;
// console.log(v);
if(v.actief == 1){
results.push(v);
}
});
// the code function which i described ->
KvkGoogleMaps(results);
}
return {
results: results,
pagination: {
more: (params.page * 30) < data.totalItemCount
}
};