In javascript you can check existence of any object just by calling a condition with it
if (data.sources[i]) {
// Do whatever you want here, even call data.sources[i].id
}
So for you code if you want to check existence of those objects it would become like this:
for (var i = 0; i < data.sources.length; i++) {
if (data.sources[i] && domainid[i]) {
selectBoxSource += '<option value="' + data.sources[i].id + '">' +
domainid[i].source + '</option>';
}
}
A higher level of validation would be
for (var i = 0; i < data.sources.length; i++) {
if (data.sources[i] && data.sources[i].id && domainid[i] && domainid[i].source) {
selectBoxSource += '<option value="' + data.sources[i].id + '">' +
domainid[i].source + '</option>';
}
}
solved How to check if object in for loop exists in JS? [closed]