0

I'm looking to make my code more efficient. I've written it like this because I can't find a way to access object properties by their respective keys. I'd ideally like to use a for loop to shorten this enormous chunk of code down. I'm open to restructuring my object, if that will help.

if (response.data[i].code == 'P01') {
  $scope.production['P01'] += response.data[i].hours;
} else if (response.data[i].code == 'P02') {
  $scope.production['P02'] += response.data[i].hours;
} else if (response.data[i].code == 'P03') {
  $scope.production['P03'] += response.data[i].hours;
} else if (response.data[i].code == 'P04') {
  $scope.production['P04'] += response.data[i].hours;
} else if (response.data[i].code == 'P05') {
  $scope.production['P05'] += response.data[i].hours;
} else if (response.data[i].code == 'P06') {
  $scope.production['P06'] += response.data[i].hours;
} else if (response.data[i].code == 'P07') {
  $scope.production['P07'] += response.data[i].hours;
} else if (response.data[i].code == 'P08') {
  $scope.production['P08'] += response.data[i].hours;
}

As you can imagine, $scope.production is an object like such:

$scope.production = {'P01' :0, 'P02' : 0, 'P03' :0};
1
  • This code does access object properties by their respective keys. Accessing a different object's properties is no different. Commented May 25, 2016 at 16:03

1 Answer 1

3
var data = response.data[i];
$scope.production[data.code] += data.hours;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.