2016-05-25 12 views
0

私のコードをより効率的にするために探しています。オブジェクトのプロパティにそれぞれのキーでアクセスする方法が見つからないため、このように記述しました。私は理想的にはforループを使用して、この膨大なコードを短くしたいと思っています。それが助けになるのであれば、私の目的を再構築するために私は開いています。あなたが想像できるようにキーによるオブジェクトプロパティへのアクセス

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; 
} 

、$ scope.productionは、オブジェクトは、そのようなようなものです:それぞれのキーで

$scope.production = {'P01' :0, 'P02' : 0, 'P03' :0}; 
+0

このコード*ありません*アクセスオブジェクトのプロパティが。別のオブジェクトのプロパティにアクセスすることも同じです。 –

答えて

3
var data = response.data[i]; 
$scope.production[data.code] += data.hours; 
関連する問題