You can use angular.isUndefined
and angular.forEach
to fine tune the code:
app.filter('categoryTranslations', function() {
return function(categories, locale) {
var translated = [];
if (!angular.isUndefined(categories)) {
angular.forEach(categories, function(category) {
angular.forEach(category.CategoryTranslations, function(translation) {
if (translation.Language.code == locale) {
category.defaultTranslation = translation;
translated.push(category);
break;
}
});
});
}
return translated;
};
});