I am facing an issue with iterating through an array and checking a condition for each element. If the condition is true, I need to return one value, otherwise another value. However, the loop is not terminating when the condition is met. Can anyone assist me in solving this problem?
$scope.bgImages = []; //contains some objects
$scope.job = []; //also contains some objects
//if both elements match, we return one value
$scope.getJobDepartmentImg = function() {
var jobDepartment = $scope.job.department;
for (var i in $scope.bgImages) {
var department = $scope.bgImages[i].departmentName;
var job_header = $scope.bgImages[i].s3ImageUrl;
if (jobDepartment === department) {
return job_header;
} else {
return default_job_header;
}
}
};
The loop is not terminating when the condition is satisfied, causing it to run continuously. Any help would be appreciated.